How to remove layer from a LayerGroup?

5,216 views
Skip to first unread message

Michal Zimmermann

unread,
Jan 16, 2013, 3:26:06 AM1/16/13
to leafl...@googlegroups.com
Hi,
I have LayerGroup which is dynamically filled by user as he/she clicks on the map. I would like to allow him/her to delete the layer created, but haven't found out how yet. There is a removeLayer() method on LayerGroup, but what should I pass as the parameter? An ID, object? I need to identify which layer shall be removed, but I have no idea how to do that.

Bryan McBride

unread,
Jan 17, 2013, 9:29:55 AM1/17/13
to leafl...@googlegroups.com
Each layer is assigned a leaflet ID, so you could iterate over the layers in the group using the eachLayer method and look for the ID to remove. Wrap everything in a function that you can call when you want to remove a layer by it's ID. Something like this:

var mylayergroup = new L.LayerGroup();

function removeMe(id) {
mylayergroup.eachLayer(function (layer) {
if (layer._leaflet_id === id){
mylayergroup.removeLayer(layer)
}
});
}

When you want to remove the layer with a leaflet_id of 50, you would just call removeMe(50).


BRYAN
Reply all
Reply to author
Forward
0 new messages