2013-07-15 54 views
2

我試圖在Openlayers中的圖層上繪製一個字符串/字符(例如,顯示路線 - >在路線附近繪製描述或樓層號)。 問題:可以向Openlayers.Vector添加標籤,但我的應用程序有一個Vector,其中包含多個幾何圖形,每個圖形都應該使用不同的字符串進行渲染。 也許存在這樣的一些幾何:layer.addFeature(新Openlayers.StringGeometry(「文本」,X,Y),還是讓我無法找到任何東西Openlayers - 在地圖上繪製字符串

有人可以給我一個提示

回答

9

要。?添加自定義文本標籤矢量圖層的功能,我建議如下:

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

+0

非常有幫助,謝謝! :) – user2312386

+0

不客氣。請考慮標記爲答案,如果這解決了你的問題:) –

+0

真的幫了我!我的投票+1 – imdadhusen