Search code examples
javascriptwebopenlayersvisibilitygml-geographic-markup-lan

How to change the order of a layer when set visible in openlayers?


I am working on a website that uses OpenLayers to draw a map and add layers of information on top of it. Everything works fine but one thing.

When I add a new layer of information, I want it to be on top of the other layers, but when add a layer of information before, I want to other layers to be on top.

My point is: The last clicked should be on top.

Right now, when I want to set the visibility of the layer to yes, this is was is triggered:

myLayer.setVisibility(true);
map.setLayerIndex(myLayer, 700);

I tried replacing the 700 by a really high value and it changed nothing. I also tried this:

map.raiseLayer(myLayer, map.layers.length);

But nothing seems to work. The layer still appears under the other layers...

Please if you have an idea, tell me. Thanks in advance for the help!

PS: This is the website (not sure if you can see it): http://labqc.wul.qc.ec.gc.ca/MetViewer/

To test, you have to select a layer on the left (images will be shown) then you click the (+) icon at the top-right to select the layer, and you add, let's say BV - Canada. Polygons will appear under the image insteaf of on top, I want the reverse situation.

.


Solution

  • You need to explicitly request that the layer redraws itself, after you have used the raiseLayer or setLayerIndex functions, as these functions do not automatically trigger a redraw: see http://trac.osgeo.org/openlayers/browser/trunk/openlayers/lib/OpenLayers/Map.js. A simple

     this.redraw();
    

    or

     map.layers[layerIndex].redraw();
    

    depending on where you are handling this from, should be enough.