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

Share:
13,981

Related videos on Youtube

Jerry Yuan
Author by

Jerry Yuan

Updated on September 16, 2022

Comments

  • Jerry Yuan
    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 whole layergroup from the map? I tried layergroup.eachLayer(function(layer) { map.removeLayer(layer);}); But, the map begins to behaviour weirdly.

  • Alexandru Pufan
    Alexandru Pufan almost 9 years
    Glad to hear this, it would be great if you accept my answer in this case.