Geolocation 更新地理定位标记,而不是使用传单api添加新标记

Geolocation 更新地理定位标记,而不是使用传单api添加新标记,geolocation,Geolocation,每次位置更新时,它都会放置一个新标记,而不是移动现有标记。我只是希望屏幕上有一个标记,而不是每次应用程序更新其位置时都放置一个新的标记(忽略最大年龄和频率,我正在测试一些东西,我知道它们不是问题)。提前谢谢 (加载地图的代码) 编辑:我已经尝试了几种解决方案,但标记仍然没有移动,它只是添加了一个新的。有什么想法吗?您可以更新现有的标记,而不是每次创建新的标记: var marker = L.icon({iconUrl: 'greendot.png'}); var movingMarker = L

每次位置更新时,它都会放置一个新标记,而不是移动现有标记。我只是希望屏幕上有一个标记,而不是每次应用程序更新其位置时都放置一个新的标记(忽略最大年龄和频率,我正在测试一些东西,我知道它们不是问题)。提前谢谢

(加载地图的代码)


编辑:我已经尝试了几种解决方案,但标记仍然没有移动,它只是添加了一个新的。有什么想法吗?

您可以更新现有的标记,而不是每次创建新的标记:

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});
我刚刚发现了一个类似的问题(答案类似):

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});