Trying to control layer order with L.geoJson but it seems that map.addLayer(layer) always adds the layer on top, if the layer was created with L.geoJson.
I've tried layer.setZIndex(foo) and this doesn't affect it, and also creating the layer with L.geoJson(data, {zIndex: foo}).
No wonder - only TileLayers implement the setZIndex method. Read the docs.
Unless we get more info, this will probably be closed as a duplicate of #2086 (always check for dupes before reporting a new bug!)
Thanks @IvanSanchez I did read that issue but not carefully enough to determine that it's the same problem...
How would I be able to control the zIndex via some other means (like CSS) ?
The only way to control zIndex in vector layers is bringToBack and bringToFront methods. This is a technical limitation — SVG lacks the concept of zIndex.
Thank you for the suggestion @mourner, I was able to do what I needed with bringToFront. Essentially sorting my layers by zIndex and calling bringToFront() on all of them, whenever a layer is added or removed.
I just stumbled over the same issue in 1.0rc1 and found it very confusing. If setZIndex has no effect in a geojson layer then it should throw an exception as it is clearly a programmer error. Of course, it would be nice to actually get setZIndex working since it _is_ possible to sort layers with the bringToBack and bringToFront methods. Can't the geoJson (or other suitable) class not override setZIndex and implement such logic?
@neothemachine
I would go for a warning instead of exceptions. Since L.geojson inherits from L.LayerGroup, and L.layerGroup has setZIndex, it's not unreasonable for user to expect that calling setZIndex on any object that inherits from L.layerGroup will not throw an error.
As for sort layers, I would love to have that. It would be nice to have a way to disable that functionality (like setZIndex to null) so that maps with many geojson layers do not pay the cost of sorting them if the user don't care about their order.
Most helpful comment
@neothemachine
I would go for a warning instead of exceptions. Since
L.geojsoninherits fromL.LayerGroup, andL.layerGrouphassetZIndex, it's not unreasonable for user to expect that callingsetZIndexon any object that inherits fromL.layerGroupwill not throw an error.As for sort layers, I would love to have that. It would be nice to have a way to disable that functionality (like
setZIndextonull) so that maps with many geojson layers do not pay the cost of sorting them if the user don't care about their order.