每次更新位置时,都会放置一个新标记,而不是移动现有标记。我只是希望屏幕上有一个标记,而不是每次应用程序更新其位置时放置一个新标记(忽略最大年龄和频率,我正在测试一些东西,我知道它们不是问题)。提前致谢。
(加载地图的代码)
function onLocationFound(e) {
var marker= L.icon({iconUrl: 'greendot.png'});
var radius = e.accuracy /2;
L.marker(e.latlng, {icon: marker}).addTo(map).bindPopup("You are within " + radius + " meters from this point").openPopup();
}
function onLocationError(e) {
alert(e.message);
}
map.on('locationfound', onLocationFound);
map.on('locationerror', onLocationError);
map.locate({watch: true, setView: true, maxZoom: 16, enableHighAccuracy: true, maximumAge:10000, frequency: 1});
编辑:我已经尝试了几种解决方案并且标记仍然没有移动,它只是添加了一个新的解决方案。有什么想法吗?
答案 0 :(得分:2)
您可以更新现有标记,而不是每次都创建新标记:
var marker = L.icon({iconUrl: 'greendot.png'});
var movingMarker = L.marker([0,0], 0, {icon: marker}).addTo(map);
function onLocationFound(e) {
var radius = e.accuracy /2;
movingMarker.setLatLng(e.latlng);
movingMarker.unbindPopup();
movingMarker.bindPopup("You are within " + radius + " meters from this point")
movingMarker.redraw();
}
function onLocationError(e) {
alert(e.message);
}
map.on('locationfound', onLocationFound);
map.on('locationerror', onLocationError);
map.locate({watch: true, setView: true, maxZoom: 16, enableHighAccuracy: true, maximumAge: 10000});
我刚发现了一个类似的问题(答案相似):update marker location with leaflet API