【问题标题】:How to reset map locator in Leaflet如何在 Leaflet 中重置地图定位器
【发布时间】:2020-10-05 10:06:30
【问题描述】:

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

    //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

【问题讨论】:

    标签: javascript leaflet location reset


    【解决方案1】:

    覆盖纬度和半径:

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

    【讨论】:

    • 谢谢!我用您提供的代码替换了 L.marker 和 L.circle 并保留了其余部分。这一切似乎都很好!
    猜你喜欢
    • 1970-01-01
    • 2013-07-19
    • 2016-01-13
    • 1970-01-01
    • 2022-01-21
    • 2021-09-08
    • 2021-02-05
    • 2019-10-04
    • 1970-01-01
    相关资源
    最近更新 更多