[GIS] Change pane of existing tileLayer in Leaflet interactively

leaflettile-layer

I'm developing a map application in Leaflet with multiple Layers (Tile-Layer and TopoJSON). I would like a button, which moves the Tile-Layer in front of the other Layers. Therefore I need to put the Tile-Layer into a pane with a z-Index > 400 (see Map Panes). This works fine when initializing the Tile-Layer, but how do I move an existing Tile-layer into another pane in the foreground after having initialised it (change "pane: 'tilePane'" to "pane: 'layerForeground'")?

map.createPane('layerForeground');
map.getPane('layerForeground').style.zIndex = 650;

var SWEmean71_00Apr =L.tileLayer('data/maps/e01_swe/tiles/3857_Mittelwert/{z}/{x}/{y}.png', {
maxZoom: 12,
minZoom: 7,
tms: false,
pane: 'tilePane',
})//.addTo(map);

Best Answer

Leaflet layers can not change panes dynamically. Panes are too tightly coupled to the initialization of the HTMLElements for the layers' containers.

You can, however, change the zIndex of a map pane dynamically. So you can set a unique pane for each of your tilelayers, and change their z-indexes when you need to change their order.