Skip to content

Loading…

removeLayer() doesnt remove layer from the map control only removed from the group layer control #5

Closed
ChrisCSmith opened this Issue · 3 comments

2 participants

@ChrisCSmith

when calling
layerControl.removeLayer(layer);
the layer is removed from the control but not from the map,
the following code appears to fix it...

removeLayer: function (layer) {
var id = L.Util.stamp(layer);
/Start inserted code/
var obj = this._layers[id];
if (obj && this._map.hasLayer(obj.layer)) {
this._map.removeLayer(obj.layer);
}
/End inserted code/
delete this._layers[id];
this._domGroups.length=0;
this._update();

return this;

}

@ismyrnow
Owner

Thanks for the opening an issue.

This behavior is consistent with how Leaflet's layer control works. I am intentionally doing it the same way to keep things as simple as possible.

@ismyrnow ismyrnow closed this
@ChrisCSmith

looking at the layer control help it states addlayer will add a layer to the layer control and removeLayer will remove the layer from the layer control. No mention of adding/removing from the map - so working as expected.

I suppose my issue is that the layer control has code that adds and removes layers from the map in method _onInputClick, So if the user selects layer 'a' in the layerControl this adds the layer to the map control, if layerControl.removeLayer(a) method is called, the option is removed from the layer control but the layer is not removed from the map. So I assume working as designed but appears that it leaves the map in a slightly inconsistent state?

anyway thanks for looking into this.

@ismyrnow
Owner

Yes, you could consider that state inconsistent. However, there could be a case where the developer desires that, so the layer control doesn't make any assumptions.

Besides being careful to remove the layer from the map when you also remove it from the control, you could achieve the same using events. The layer control fires an event called "overlayremove", which you could listen to, and remove the layer also from the map when it's fired.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.