How to remove a layerGroup in leaflet?
13,981
Solution 1
You can simply use the removeLayer method, like this:
map.removeLayer(layerGroup)
Take a look at this plunker (click on the map to remove the layer):
http://plnkr.co/edit/jSUE8ft9p7odLhQGeOVa
Solution 2
layergroup.eachLayer(function(layer) { **map**.removeLayer(layer);});
use
layergroup.eachLayer(function(layer) { **layergroup**.removeLayer(layer);});
instead
Related videos on Youtube
Author by
Jerry Yuan
Updated on September 16, 2022Comments
-
Jerry Yuan over 1 year
it is easy to add a layerGroup to a leaflet map by simply calling
layergroup.addTo(map)
. How I can I remove the wholelayergroup
from the map? I triedlayergroup.eachLayer(function(layer) { map.removeLayer(layer);});
But, the map begins to behaviour weirdly. -
Alexandru Pufan almost 9 yearsGlad to hear this, it would be great if you accept my answer in this case.