Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Openlayers - Draw String on Map

Im trying to draw a String/Character on a layer in Openlayers (eg displaying a Route --> draw description or floor number near the Route). Problem: It is possible to add a Label to a Openlayers.Vector, but my Application has one Vector with multiple geometries in it, which should be rendered with a different String each. Maybe there exists some Geometry like this: layer.addFeature(new Openlayers.StringGeometry("text", x,y) or so. I couldnt find anything.

Can someone give me a hint?

like image 693
user2312386 Avatar asked Dec 27 '22 00:12

user2312386


1 Answers

To add custom text label to the features of Vector layer, I suggest the following:

1) add StyleMap to your Vector layer as such:

var vectorLayer = new OpenLayers.Layer.Vector("Vector", 
{
    styleMap: new OpenLayers.StyleMap(            
    {
        label : "${labelText}",                    
        fontColor: "blue",
        fontSize: "12px",
        fontFamily: "Courier New, monospace",
        fontWeight: "bold",
        labelAlign: "lc",
        labelXOffset: "14",
        labelYOffset: "0",
        labelOutlineColor: "white",
        labelOutlineWidth: 3
    })
});

note that labelText in this style map says that text for this label will be taken from corresponding feature attribute.

2) For each feature you add to your layer specify the attributes having labelText defined:

var features = [];
var pt = new OpenLayers.Geometry.Point(0, 0);
features.push(new OpenLayers.Feature.Vector(pt, {labelText: "This is my label"}));
vectorLayer.addFeatures(features);

The only limitation with this solution is that you will have to add feature for each point and not able to use OpenLayers.Geometry.MultiPoint.

like image 197
the_virt Avatar answered Jan 13 '23 02:01

the_virt