我试图在 Openlayers 中的图层上绘制字符串/字符(例如显示路线 --> 在路线附近绘制描述或楼层号)。 问题:可以将标签添加到 Openlayers.Vector,但我的应用程序有一个包含多个几何图形的向量,每个几何图形应该使用不同的字符串呈现。 也许存在这样的几何图形:layer.addFeature(new Openlayers.StringGeometry("text", x,y) 左右。我找不到任何东西。
有人可以给我提示吗?
最佳答案
要为矢量图层的特征添加自定义文本标签,我建议如下:
1) 将 StyleMap
添加到您的 Vector 层:
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
})
});
请注意,此样式映射中的 labelText
表示此标签的文本将从相应的要素属性中获取。
2) 对于添加到图层的每个要素,指定定义了 labelText
的属性:
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);
此解决方案的唯一限制是您必须为每个点添加特征并且不能使用 OpenLayers.Geometry.MultiPoint
。
关于javascript - Openlayers - 在 map 上绘制字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17653471/