Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Leaflet: removeLayer by id do not work

Tags:

leaflet

I am trying to remove element from map: Work code:

var x = L.imageOverlay(fullURL, xbounds).addTo(Window.map);
Window.map.removeLayer(x);

Do not work:

var x = L.imageOverlay(fullURL, xbounds).addTo(Window.map);
Window.map.removeLayer(1);

By the docs it have method for removing elements by IDs

like image 371
Dmitry Bubnenkov Avatar asked Sep 07 '25 18:09

Dmitry Bubnenkov


1 Answers

Actually, map.removeLayer() accepts only a layer (as in your first code).

It is layerGroup.removeLayer() that can also accept an ID.

This "ID" is automatically defined by Leaflet, and you can retrieve it using L.stamp(layer).

var layerGroup = L.layerGroup().addTo(map)
var x = L.marker(coordinates).addTo(layerGroup);
var x_id = L.stamp(x); // Retrieve the x layer ID
layerGroup.removeLayer(x_id);

Demo: https://jsfiddle.net/3v7hd2vx/65/

like image 78
ghybs Avatar answered Sep 13 '25 03:09

ghybs