问题描述
我的地图定位器可以工作,但是我不确定如何重设标记,以便当用户进行地理定位时它可以重设位置。目前,我的代码如下:
//map locator
map.locate({setView: true,maxZoom: 16});
function onLocationFound(e) {
var radius = e.accuracy;
L.marker(e.latlng).addTo(map)
.bindPopup("Vous êtes ici").openPopup();
L.circle(e.latlng,radius).addTo(map);
}
map.on('locationfound',onLocationFound);
function onLocationError(e) {
alert(e.message);
}
map.on('locationerror',onLocationError);
// end of geolocator with marker
解决方法
覆盖纬度和半径:
var marker = null;
var circle = null;
//map locator
map.locate({setView: true,maxZoom: 16});
function onLocationFound(e) {
var radius = e.accuracy;
if(!marker){
marker = L.marker(e.latlng).addTo(map);
circle = L.circle(e.latlng,radius).addTo(map);
}else{
marker.setLatLng(e.latlng);
circle.setLatLng(e.latlng);
circle.setRadius(radius);
}
marker.bindPopup("Vous êtes ici").openPopup();
}