2016-01-22 27 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”结合起来。你让我的周末成为我的朋友。非常感谢。 –