Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Layer order changing when turning layer on/off

I have two geoJson layers being loaded - both layers are the same data for testing purposes, but being drawn from two different json files. When I turn the layers on and off in the layer controller, the draw order of the layers change.

Any ideas why this is happening?

I have put my code into a JSFiddle: http://jsfiddle.net/lprashad/ph5y9/10/ and the JS is 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

like image 203
Lee Avatar asked May 30 '13 02:05

Lee


1 Answers

While searching I happened upon 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 the application of autoZIndex:

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

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

I'm not sure which annoys me more. This incredible limitation or the fact that Leafet documentation doesn't point it out.

like image 180
Rikaelus Avatar answered Dec 25 '22 10:12

Rikaelus