2012-12-11 171 views
0

一些信息: 我得到了一個主層(地圖),即時通過線串從JSON結果中獲取點之間的線條。Openlayers自定義標記

(問題) 我跟着一個例子在線上如何自定義我加入點。但這不起作用。 (只看底部的功能。)

代碼:

//'listOfPoints' is an array containing all the point objects. 

var pointmap = new OpenLayers.Geometry.LineString(listOfPoints); 

    var lastpoint = listOfPoints[listOfPoints.length -1]; 


    var vesselLayer = new OpenLayers.Layer.Vector(data.bridge_name); 

    if (lastpoint != null) { 
     var markerLayer = getPOI(lastpoint); 
     vesselLayer.addFeatures([pointmap,markerLayer]); 
    } else { 
     vesselLayer.addFeatures([new OpenLayers.Feature.Vector(pointmap)]); 
    } 

// Function for creating a marker and returning it to the caller. 

function getPOI(point) { 

//This was also tried without the "Style" property. (Only the externalGraphic line) 
var temp_feature = new OpenLayers.Feature.Vector(
     point, 
     style: { externalGraphic: '/assets/img/marker.png', graphicHeight: 16,  graphicWidth: 16, graphicXOffset:8, graphicYOffset:8 } 
    );  

return temp_feature; 
} 

回答

1

乍一看,有錯誤temp_feature定義:

var temp_feature = new OpenLayers.Feature.Vector(
    point, 
    null, 
    { 
     externalGraphic: '/assets/img/marker.png', 
     graphicHeight: 16, 
     graphicWidth: 16, 
     graphicXOffset:8, 
     graphicYOffset:8 
    } 
); 

它閱讀並遵照API文檔是非常有用的: http://dev.openlayers.org/docs/files/OpenLayers/Feature/Vector-js.html#OpenLayers.Feature.Vector.OpenLayers.Feature.Vector

OpenLayers.Feature.Vector有三個參數,幾何形狀,物體的屬性(這是你的情況,正弦空e你沒有任何屬性),並有風格的對象。

我還沒有測試過這個代碼,可能還有其他問題。

而關於一般使用JavaScript,從來就沒有這樣的東西作爲

style: { externalGraphic: '/assets/img/marker.png', graphicHeight: 16,  graphicWidth: 16, graphicXOffset:8, graphicYOffset:8 } 

對象總是括號內定義:

{style: { externalGraphic: '/assets/img/marker.png', graphicHeight: 16,  graphicWidth: 16, graphicXOffset:8, graphicYOffset:8 }} 
+0

太謝謝你了!我其實在我的腳本中有{style:{}}。但我在粘貼中出於某種原因改變了它。抱歉!所以這個null參數訣竅!謝謝! – Christer