Clear Marker Layers (Leaflet )

前端 未结 3 2001
春和景丽
春和景丽 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: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();
    

提交回复
热议问题