Clear Marker Layers (Leaflet )

前端 未结 3 1999
春和景丽
春和景丽 2020-12-16 16:54

I am using Leaflet library and stuck with following issue: To generate Map i am calling map function on button click. So on each generatemap function call, I want to clear

相关标签:
3条回答
  • 2020-12-16 17:08

    All simple:

    map.eachLayer((layer) => {
      layer.remove();
    });
    

    from https://leafletjs.com/reference-1.0.3.html#map-event

    0 讨论(0)
  • 2020-12-16 17:17

    You can use this

    $(".leaflet-marker-icon").remove(); $(".leaflet-popup").remove();

    0 讨论(0)
  • 2020-12-16 17:24

    Instead of adding the markers directly to your map, add them to a L.LayerGroup

    Whenever you want, you can remove your markers calling the clearLayers method

    var layerGroup = L.layerGroup().addTo(map);
    
    // create markers
    L.marker().addTo(layerGroup);
    
    // remove all the markers in one go
    layerGroup.clearLayers();
    
    0 讨论(0)
提交回复
热议问题