Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Layer.remove() should properly remove from parent LayerGroup #9209

Open
4 tasks done
quarl opened this issue Jan 7, 2024 · 3 comments
Open
4 tasks done

Layer.remove() should properly remove from parent LayerGroup #9209

quarl opened this issue Jan 7, 2024 · 3 comments
Labels
bug needs triage Triage pending

Comments

@quarl
Copy link

quarl commented Jan 7, 2024

Checklist

  • I've looked at the documentation to make sure the behavior isn't documented and expected.
  • I'm sure this is an issue with Leaflet, not with my app or other dependencies (Angular, Cordova, React, etc.).
  • I've searched through the current issues to make sure this hasn't been reported yet.
  • I agree to follow the Code of Conduct that this project adheres to.

Steps to reproduce

Code:

  1. Create a LayerGroup and add to the map.
  2. Create some layer, e.g. a Marker, and add to the LayerGroup.
  3. Call layer.remove()
  4. Remove the LayerGroup.
  5. Add the LayerGroup back.
const map = L.map('map', {
	layers: [L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png')],
	center: [51, 0.02], zoom: 14
});

const layerGroup = L.layerGroup().addTo(map);
L.control.layers(null, { 'Notice that clicking off and on introduces a middle marker': layerGroup }, {collapsed: false}).addTo(map);
const marker1 = L.circleMarker([51, 0.01]).addTo(layerGroup);
const marker2 = L.circleMarker([51, 0.02]).addTo(layerGroup);
const marker3 = L.circleMarker([51, 0.03]).addTo(layerGroup);
// Layer.remove() is buggy.  It removes from the Map but not the
// LayerGroup that it's in.  If we remove the LayerGroup and add the
// LayerGroup back (for example using L.Control.Layers), then the marker 
// reappears; whereas layerGroup.removeLayer() correctly removes from
// both LayerGroup and Map.
marker2.remove();

Expected behavior

I expected that layer.remove() removes from the map AND from the layerGroup that it's in, such that it remains removed if the layerGroup is removed and re-added.

Current behavior

As of Leaflet 1.9.4, Layer.remove() removes the layer from layer._map._layers, but does not remove from layerGroup._layers, for the layerGroup that it's in.
The layer gets removed from DOM and visually disappears.
However, if you remove the LayerGroup and add it back (for example via Layers Control), then the supposedly removed layer reappears (because it was never removed from layerGroup._layers).

Minimal example reproducing the issue

https://plnkr.co/edit/iYrvKIpBwRsUtYd2?preview

Environment

  • Leaflet version: 1.9.4
  • Browser (with version): Chrome 120.0.6099.129 , Firefox 121.0
  • OS/Platform (with version): Mac
@quarl quarl added bug needs triage Triage pending labels Jan 7, 2024
@bre01
Copy link

bre01 commented Jan 8, 2024

According to the docs, layer only removed from map but kept in group is expected behavior.
https://leafletjs.com/reference.html#circlemarker-remove.
You can instead use removeFrom method to remove the layer from group.
https://leafletjs.com/reference.html#circlemarker-removefrom

@quarl
Copy link
Author

quarl commented Jan 8, 2024

Regardless of whether it's what's currently documented, it seems like unintuitive behavior to me. It puts the objects into an inconsistent state.

@IvanSanchez
Copy link
Member

You gotta realize that a L.Layer can belong to several L.LayerGroups at once, and that there can be nested L.LayerGroups.

The logic could be different, but there's no one-size-fits-all solution that will make everybody happy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug needs triage Triage pending
Projects
None yet
Development

No branches or pull requests

3 participants