-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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 order in L.Control.Layers
is not preserved with GeoJson/FeatureGroup
#2086
Comments
Hm, if Control.Layers implies sort, it really should take a list rather than an object. Sure, the 'not necessarily sorted' behavior of objects in browsers ends up being 'mostly sorted', but it's also tricky/impossible to reorder properties in an object without creating a new one. |
I too don't like that L.Control.Layers relies on |
Here's the issue: https://github.com/Leaflet/Leaflet/blob/master/src/control/Control.Layers.js#L140 |
I think the fix will fit well into a bigger refactoring of layers code: all layers will fire add/remove event, and the control will subscribe to those events of its layers instead of listening to all layers added/removed. |
Layer order is also not preserved when using So doing something like... L.control.layers({
image1: L.imageOverlay(url, bounds);
image2: L.imageOverlay(url, bounds);
}).addTo(map); and then enabling |
Pushed to 0.8 |
This bug is critical, since it is a feature that is broken in leaflet. It's impossible to use ImageOverlay layers along TileLayers, because the ImageOverlay are ALWAYS on bottom and so "invisible". |
@luca76 this is weird since ImageOverlay should be always on top of TileLayer, as the first are placed in overlay pane while the last are in tile pane. Can you make a JSFiddle that reproduces this? |
I guess the problem I am having, as I posted to Stackoverflow is because of this bug? http://stackoverflow.com/questions/23141602/leaflet-js-wms-layer-zoom-not-working-on-double-click |
I presume I am experiencing this. Race conditions seem to apply when loading multiple remote layers. More complex geometry loads last and therefore covers simpler (smaller filesize) data. What I don't understand is that if I simply create the GeoJSON layer without its data and add it to the map, and then addData() via AJAX this still seems to destroy layer order. Or am I getting something wrong? |
With Leaflet 0.8 the ImageOverlay is on top of the TileLayers, but on top of all TileLayers: var baseMaps = {
"OSM": layerOSM
};
var overlayMaps = {
"Image": layerIMG,
"Labels": layerTonerLabels
};
L.control.layers(baseMaps, overlayMaps).addTo(map); In this example the image is above the labels, where it should be vice verca. Is it not possible to put the image also in the tile pane? |
@prebm you can specify pane for your ImageOverlay through |
@mourner as I can see adding the ImageOverlay to the tilePane does not assign a z-index to the ImageOverlay, hence the ImageOverlay is not visible at all. After inspecting with Firebug i saw, that the would be in the correct order and also the other TileLayers had the correct z-indexes to allow the ImageOverlay to be "between" them. Adding the z-value manually then produced the right outcome. |
Yeah, possible via CSS or by adding ImageOverlay after all tile layers. But I should probably add zIndex option to ImageOverlay. |
Ok, thanks |
I think this is still valid? I was able to work around this by removing the old geoJSON layer and replacing it with a new one (ostensibly with a new internal ID in a proper order) and it appears in the correct place. |
Hi folks. I'm also experiencing this with geojson featuregroups. Lots of overlapping polygons and the zindex changes confuse the user. Can anyone suggest a canonical/somewhat-future-proof method for maintaining group display order ? |
Afraid not. I build an array of layers in their correct order, and cycle through the whole array each time layers are added, bringing every layer in turn to the front. Horrible, but it works. I have no alternative at this stage. |
I'm basically doing what tomchadwin has except more explicit, since I've just kind of grown into the code. If I get around to refactoring it I'll either use a similar array or probably make my own little Javascript snippet to manage my own control interface and stick it within the map. |
If anyone here is using Leaflet 1.0.0 you should be able to use the new
You can see this inaction with lots of overlapping polygons here. |
patrickarlt thought I'd let you know I implemented the pattern you outlined above. This does lay out the order of the panes properly -- panes and their associated layer group are added to the map such that the pane appears beneath successively added panes. However, when using l.control.layers.addOverlay to then associate L.control.layers with panes on the map --> zero order to the layer names as they appear in the control seems possible. Half way there! Any suggestions ? |
I look a look at the code. It looks like layers are stored internally in the control by the layers internal Leaflet ID https://github.com/Leaflet/Leaflet/blob/master/src/control/Control.Layers.js#L115-L121. The internal ID generator gets called A LOT. For example in my JS Bin http://jsbin.com/vocigipive/1/edit?html,css,js,output it is already at 177 by the time everything gets done. Since all browsers sort objects when looping like @mourner I'm wondering if this could be refactored to use an array of layers internally rather then an object. // convert basemaps and overlays to ordered arrays internally
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'}),,
}).addTo(map);
// add a new position property to control where the added layer falls in the order. Default would be at the bottom.
control.addOverlay(layer, name, position); |
@patrickarlt I think that's a great approach. I would be up for a PR on this. |
@patrickarlt up for working on this before 1.0 or should I postpone? :) |
Huh, I already made a PR for this... |
When I do
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:
http://jsfiddle.net/km5H9/1/
The text was updated successfully, but these errors were encountered: