Reorder layers when turning a layer on / off

I have two loadable geoJson layers - both levels are the same data for testing, but are extracted from two different json files. When I turn layers on and off in the level controller, the order in which the layers are drawn changes.

Any ideas why this is happening?

I put my code in JSFiddle: http://jsfiddle.net/lprashad/ph5y9/10/ , and JS below:

//styling for watersheds_copy var Orange = { "color": "#ff7800", "weight": 5, "opacity": 0.65 }; var Water_Orange = L.geoJson(watersheds_copy, { style: Orange }); Water_Orange.addData(watersheds_copy); //these are blue var Water_blue = L.geoJson(watersheds, {}); Water_blue.addData(watersheds); //This sets the inital order - last in layer list being on top. Except minimal - tile layer is always on bottom var map = L.map('map', { center: [41.609, -74.028], zoom: 8, layers: [minimal, Water_Orange, Water_blue] }); var baseLayers = { "Minimal": minimal, "Night View": midnight }; //This controls the order in the layer switcher. This does not change draw order var overlays = { "Water_Orange": Water_Orange, "Water_blue": Water_blue }; L.control.layers(baseLayers, overlays).addTo(map); 

LP

+8
leaflet geojson
source share
3 answers

While searching, I ended up on this site that shows some of the leaflet code: http://ruby-doc.org/gems/docs/l/leaflet-js-0.7.0.3/lib/leaflet/src/control/Control_Layers_js. html

In it, I found this condition for using autoZIndex:

  if (this.options.autoZIndex && layer.setZIndex) { this._lastZIndex++; layer.setZIndex(this._lastZIndex); } 

TileLayer is the only type of layer that has the setZIndex function, so obviously autoZIndex only works there.

I'm not sure what annoys me more. This is an incredible limitation, or the fact that Leafet documentation does not indicate this.

+2
source share

At least on 0.7.2, I had to use bringToFront in the map.on('overlayadd') . autoZIndex: false in my case did not work. Commentary on this problem may explain the reason.

+1
source share

This does not apply to the layers of L. Geojson. As far as I can tell, this is true for all Leaflet layers with level control. The last layer is included, just on top. I do not think that this is also a mistake. This is a predictable behavior that I use and depends on when I develop maps with level control ...

0
source share

All Articles