2016-01-22 101 views
2

你好傢伙我試圖更新我的標記位置,但我沒有設法找出如何刪除舊的。我所得到的只是標記的「歷史」。我沒有任何可以幫助我的例子。我希望有人會給我一個線索繼續。 非常感謝Per Liedman這個出色的工作。Leaflet-GeoJson和多個標記的實時插件

    var shipLayer = L.layerGroup(); 
        var ships = L.icon({ 
         iconUrl: '../icons/ship-icon.png', 
         iconSize: [30, 30] 
        }); 
        var realtime = L.realtime({ 
         url: 'jsonServlet/ships.json', 
         crossOrigin: true, 
         type: 'json' 
        }, { 
         interval: 5 * 1000, 
         pointToLayer: function (feature, latlng) { 

          marker = L.marker(latlng, {icon: ships}); 
       marker.bindPopup('mmsi: ' + feature.properties.mmsi + 
           '<br/> course:' + feature.properties.hdg+ 
           '<br/> speed:' + feature.properties.sog); 
           marker.addTo(shipLayer); 
           return marker; 
         }        
        }); 
        controlLayers.addOverlay(geojson, 'Ships'); 
+0

您可以發佈您ships.json的例子嗎?正如[這個錯誤報告](https://github.com/perliedman/leaflet-realtime/issues/10)所解釋的,「Leaflet Realtime會嘗試尋找一個被調用的屬性ID並使用它來移除舊標記」。如果你能夠改變json的創建過程,你應該嘗試一下。 – chrki

+0

{「type」:「FeatureCollection」,「crs」:{「type」:「name」,「properties」:{「name」:「urn:ogc:def:crs:OGC:1.3:CRS84」}} 「特徵」:[{ 「幾何」:{ 「座標」:[48.517708,18.255447], 「類型」: 「點」}, 「類型」: 「功能」, 「屬性」:{ 「幾何/座標/經度」 : 「48.517708」, 「幾何/類型」: 「點」, 「MMSI」: 「512131345」, 「幾何/座標/緯度」: 「18.255447」, 「HDG」: 「108」, 「嵌齒」: 「108」 ,「sog」:「30.0」,「type」:「Feature」}}]}這個json中的ID是mmsi(unigue商船碼) –

回答

2

默認情況下,L.realtime使用功能的id屬性來更新它。正如您在評論中解釋的那樣,您船舶的標識符位於GeoJSON功能的mmsi屬性中,並且沒有id。您將需要這options添加到您的L.realtime設置:

getFeatureId: function(featureData){ 
    return featureData.properties.mmsi; 
} 

在這裏看到:https://jsfiddle.net/chk1/hmyxb6ur/

+0

好的。這是問題所在。我無法將「pointToLayer」和「getFeatureId」結合起來。你讓我的週末成爲我的朋友。非常感謝。 –