Openlayers - 在地图上绘制字符串

use*_*386 3 javascript gis map openlayers openstreetmap

我试图在Openlayers中的一个图层上绘制一个字符串/字符(例如,在路线附近显示一条路线 - >描述描述或楼层编号).问题:可以将一个Label添加到Openlayers.Vector,但我的Application有一个Vector,其中包含多个几何,每个都应该使用不同的String进行渲染.也许存在一些像这样的几何:layer.addFeature(新的Openlayers.StringGeometry("text",x,y)左右.我找不到任何东西.

有人能给我一个暗示吗?

the*_*irt 10

要向Vector图层的要素添加自定义文本标签,我建议如下:

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
    })
});
Run Code Online (Sandbox Code Playgroud)

请注意,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);
Run Code Online (Sandbox Code Playgroud)

此解决方案的唯一限制是您必须为每个点添加功能而无法使用OpenLayers.Geometry.MultiPoint.