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?
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With