要为向量层的特征添加自定义文本标签,我建议如下:
1) 将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。