Leaflet: markers shared by multiple layers member of L.Control.Layers are disabled even if the remaining layers include them

Created on 25 Mar 2012  路  7Comments  路  Source: Leaflet/Leaflet

In leaflet 0.3.1, when the same marker is added to more than one layer, all of which are added to L.Control.Layers as overlays, and one of the overlays is disabled, the marker is disabled.

e.g.

var groupA = new L.LayerGroup(),
    groupB = new L.LayerGroup();

map.addLayer(groupA).addLayer(groupB);

var overlays = {
 "A features": groupA,
 "B features": groupB,
};

map.addControl(new L.Control.Layers(null, overlays));

var marker = new L.Marker(new L.LatLng(1.0, 1.0));
groupA.addLayer(marker);
groupB.addLayer(marker);

Now, whenever the "A features" overlay is disabled, the marker is removed, even if the "B features" overlay is still enabled. Disabling and re-enabling "B features" does re-add the marker.

bug

Most helpful comment

I wrote a workaround that does the trick for me. Perhaps this new behaviour should be hidden behind some forceReadd option or something, otherwise I might think its performance impact is not welcome.

All 7 comments

You need a newline before the ```javascript :)

Thanks, fixed it.

I can see where such behavior could be useful (different sets of markers, some overlapping), but it's quite hard to fix as layers in Leaflet are currently completely independent from each other... I'll think about this, thanks for the report.

I wrote a workaround that does the trick for me. Perhaps this new behaviour should be hidden behind some forceReadd option or something, otherwise I might think its performance impact is not welcome.

I don't see an easy way to implement this, so if there's a workaround for people that need it, I think I can close this for now.

      I wrote a workaround that does the trick for me. Perhaps this new behaviour should be hidden behind some forceReadd option or something, otherwise I might think its performance impact is not welcome.

@rgeissert can you share your workaround, please?

@MaxiPigna I think the workaround is in the cross-referenced commit here: https://github.com/rgeissert/Leaflet/commit/e273f1bb0aeef485d07c2814429a28ea9ea07474

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zdila picture zdila  路  3Comments

ssured picture ssured  路  3Comments

edmsgists picture edmsgists  路  3Comments

brambow picture brambow  路  3Comments

walterfn2 picture walterfn2  路  4Comments