question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Layer order in `L.Control.Layers` is not preserved with GeoJson/FeatureGroup

See original GitHub issue

When I do

L.control.layers({
    'OSM': L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png').addTo(map)
}, {
    'Circle': L.circle([53, 4], 111111),
    'Polygon': L.polygon([[48, -3], [50, -4], [52, 4]]),
    'GeoJSON': L.geoJson(geojson)
}, {
    collapsed: false
}).addTo(map);

I expect the order of the overlays to be as I supplied them, however, GeoJSON and FeatureGroup layers are moved up top. This is because the FeatureGroup gets its _leaflet_id while it’s created, while the other layers get it while they’re added to Control.Layers. FeatureGroup is stamped because it’s passed as the context to layer.on in FeatureGroup.js:19.

Not sure how to fix this:

  • stamping each layer while initializing? Seems dirty.
  • Different sorting mechanism in Control.Layers?

http://jsfiddle.net/km5H9/1/

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:25 (16 by maintainers)

github_iconTop GitHub Comments

1reaction
patrickarltcommented, Jan 25, 2016

If anyone here is using Leaflet 1.0.0 you should be able to use the new pane option to control this:

map.createPane('bottom');
map.createPane('middle');
map.createPane('top');

// now pane order controls layer ordering not the control!
L.control.layers({
    'OSM': L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png').addTo(map)
}, {
    'bottom': L.geoJson(geojson, {pane: 'bottom'}),
    'middle': L.geoJson(geojson, {pane: 'middle'}),
    'top':  L.geoJson(geojson, {pane: 'top'}),,
}, {
    collapsed: false
}).addTo(map);

You can see this inaction with lots of overlapping polygons here.

0reactions
jietercommented, Apr 2, 2016

Huh, I already made a PR for this…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Changing layer order in Leaflet - GIS Stack Exchange
Basically you remove all layers and then re-add them in ascending z-index order. So loop through all your overlay layers, and call map....
Read more >
Leaflet Layer Control: Ordering - Stack Overflow
In Leaflet 1.0.0-rc1, internal layer IDs are not used at all, and the layers control uses the order they were added to it....
Read more >
L.Control.Layers
If true , the control will assign zIndexes in increasing order to all of its layers so that the order is preserved when...
Read more >
How to make layer control visible at all times? Not a icon?
I would like to show as a opened layer control without the need for the user to mouse over it, so that it...
Read more >
L.Control.Layers - WRLD3D
The layers control gives users the ability to switch between different base ... all of its layers so that the order is preserved...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found