20

Leaflet maintains that the Map panes elements contain all layers added to the map. The difference between many of the Map panes is strictly the z-index order of layering.

I would like to use a combination of lvector.CartoDB layers, which are essentially overlayPane layers, with TileLayer, such as GeoIQ Acetate-bg and labels.

This is the ordering of the elements as they are added the map:

tileLayer1 = new L.TileLayer();
map.add(tileLayer1);  // add first layer to map

cartoDBLayer1 = new lvector.CartoDB();
cartoDBLayer.setMap(map); // add second layer to map

tileLayer2 = new L.TileLayer();
map.add(tileLayer2);  // add third layer to map

What returns is a map with layers in this order:

tileLayer1,tileLayer2,cartoDBLayer1

tileLayer1 and tileLayer2 are situated in the HTMLElement: TilePane and cartoDBLayer1 is in HTMLElement: overlayPane.

Is there any way to force cartoDBLayer1 to render in the TilePane, such that it falls in order of the z-index that it is added to the map in...

i.e.

z-index[0]:tileLayer1
z-index[1]:cartoDBLayer1
z-index[2]:tileLayer2 

1 Answer 1

21

Update Sept 2014

Leaflet now supports setting the zIndex. Thanks to @knutole in the comments for letting me know.

Old Answer

Have you seen this issue created one the LeafLet github repo:

https://github.com/Leaflet/Leaflet/issues/167

try to use

 addLayer(layer,true);

to add a tile layer to the bottom. I'm afraid that's all there is and this second optional argument is not even documented.

5
  • 1
    +1 for mentioning an undocumented feature here, thus documenting it. :)
    – fgysin
    Commented Nov 21, 2012 at 14:01
  • 9
    There is now a layer.setZIndex(zIndex) function available.
    – knutole
    Commented Sep 4, 2014 at 10:31
  • 5
    Thanks - layer.setZIndex(99) doesn't seem to work for me with vector symbology (line and polygon features), but using layer.bringToFront() did the trick. Commented Sep 10, 2015 at 16:35
  • The equivalent call for L.Marker is layer.setZIndexOffset(...) Commented Feb 19, 2020 at 0:08
  • @diachedelic unfortunately CircleMarker aren't Marker, they're Path... I have a CircleMarker and I can't make it display above a permanent Tooltip
    – jokoon
    Commented Oct 19, 2023 at 10:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.