How to remove a layerGroup in leaflet?

18.8k views Asked by At

it is easy to add a layerGroup to a leaflet map by simply calling layergroup.addTo(map). How I can I remove the whole layergroup from the map? I tried layergroup.eachLayer(function(layer) { map.removeLayer(layer);}); But, the map begins to behaviour weirdly.

2

There are 2 answers

1
Alexandru Pufan On

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

0
xuehongyan On
layergroup.eachLayer(function(layer) { **map**.removeLayer(layer);});

use

layergroup.eachLayer(function(layer) { **layergroup**.removeLayer(layer);});

instead