我想在Openlayers中的一个图层上绘制一个字符串或字符(例如显示路径->绘制描述或楼层号码)。问题是:可以将标签添加到Openlayers.Vector中,但我的应用程序有一个包含多个几何形状的向量,每个几何形状应该用不同的字符串呈现。也许存在这样的几何形状:layer.addFeature(new Openlayers.StringGeometry("text", x,y),但我找不到任何东西。
请问有人能给我一些提示吗?
StyleMap
: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
。