叶片实时插件与GeoJson和多个标记

3
你好,我正在尝试更新我的标记位置,但是我无法找到如何删除旧标记的方法。我只能得到一个标记的“历史记录”。我没有找到任何能帮助我的示例。我希望有人能给我一些线索。 非常感谢Per Liedman所做的出色工作。
                    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');

1
你能提供一下你的 ships.json 的样例吗?正如 这个 bug 报告 所解释的那样,“Leaflet Realtime 将尝试查找一个叫做 id 的属性并使用它来删除旧标记”。如果你能够改变 json 创建过程,就应该尝试一下。 - chrki
1
{"type":"FeatureCollection","crs":{"type":"name","properties":{"name":"urn:ogc:def:crs:OGC:1.3:CRS84"}},"features":[{"geometry":{"coordinates":[48.517708,18.255447],"type":"Point"},"type":"Feature","properties":{"geometry/coordinates/longitude":"48.517708","geometry/type":"Point","mmsi":"512131345","geometry/coordinates/latitude":"18.255447","hdg":"108","cog":"108","sog":"30.0","type":"Feature"}}]} 此 JSON 中的 ID 是 mmsi(唯一的商船代码) - kostas M.
1个回答

3

默认情况下,L.realtime使用要更新的要素的id属性。根据您在评论中的解释,船只的标识符位于GeoJSON要素的mmsi属性中,而没有id。因此,您需要将其添加到options中的L.realtime设置中:

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

看这里:https://jsfiddle.net/chk1/hmyxb6ur/

1
好的,问题就在这里。我无法将“pointToLayer”和“getFeatureId”结合起来。你让我的周末变得美好,朋友。非常感谢。 - kostas M.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接