如何在Leaflet中重置地图定位器

问题描述

我的地图定位器可以工作,但是我不确定如何重设标记,以便当用户进行地理定位时它可以重设位置。目前,我的代码如下:

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

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...