31

I have a marker layer, two vector layers and two raster layers. The marker layer gets covered by the two raster layers.

Is there a way to move the marker layer always on top?

Edited: The order of Z-indexes works well in the vector layer, but when I add another WMS raster layer on the map with a lower Z-index, the raster layer still covers the vector layer.

Edited: Could anyone point out the difference of the rendering order in terms of "LayerIndex" and "Z-Index" of a Layer? I think the layer index deal with the overlay order and the Zindex deals with the feature order in a vector layer, am I right?

3
  • I think layerindex and z-index are interchangeable terms in OpenLayers. They are used in the same context in the documentation, ex: dev.openlayers.org/releases/OpenLayers-2.11/doc/apidocs/files/…
    – Casey
    Commented Oct 4, 2011 at 18:24
  • Hello @Casey I appreciate your example. However, I do not think the layerindex and z-index are interchangeable. For my own experience the setZindex does not work but the setIndex works. I notice that: 1. the Z-index example in openlayer works for a feature in a vector layer, and it is enabled by rendererOptions: {zIndexing: true}. 2.From your example for the raster image layer, you can see the layer render order is based on what you see on the map control, which depends on the layer index (as your answer code). 3. The layer index is set by the order I add on new layer (based layer as 0...)
    – Seen
    Commented Oct 5, 2011 at 1:52
  • 1. I think you are right. 2 & 3. Right, but you can override the default behavior by calling the setLayerIndex method, which adjusts the z-index of a layer. This will effect the order of the layers in the map control and the drawing order of the layers.
    – Casey
    Commented Oct 5, 2011 at 2:38

4 Answers 4

25

Setting the z-index for each layer should help: http://www.openlayers.org/dev/examples/ordering.html

EDITED

The example I originally linked to isn't too helpful. You want to set the layerindex/z-index of the layers, not the makers within the layers (as the ordering example showed). I put together an example (based off this example) showing custom layer indexing. Notice that when you turn on the Canada Raster, the Marker layer stays on top.

Here is the relevant code:

map.setLayerIndex(dm_wms, 0); //set the image overlay to the bottom
map.setLayerIndex(markers, 99); //set the marker layer to an arbitrarily high layer index

The documentation for the setLayerIndex code is here: http://dev.openlayers.org/releases/OpenLayers-2.11/doc/apidocs/files/OpenLayers/Map-js.html#OpenLayers.Map.setLayerIndex

1
  • 2
    the example links to a malware.
    – Bazinga777
    Commented Nov 28, 2018 at 9:29
9

You can do it , if you want your vector layer always top with simply code...

var vecLyr = map.getLayersByName('VectorLayer')[0];
map.raiseLayer(vecLyr, map.layers.length);

May it be easy!

6

none of the above worked for me, but simply doing this whenever I add a new layer worked a treat to keep my marker layer on top:

myMarkerLayer.setZIndex( 1001 );

1
  • This was the only answer that worked for me too. None of the other ones worked. Commented Jan 24, 2018 at 7:44
3

You can think of adding a layer to map as of laying a new sheet on top of existing ones. If the new sheet is not transparent it will hide all those below.

Unfortunately OpenLayers does not have an insertLayerAt method, so you have to reorder your layers after insertion. The solution is then to add the layer and then move it down the stack to the desired depth or move the one that you want on top back to the top.

You can do that as follows:

// add your wms as usual
var yourWMSLayer = new ....
map.addLayer(yourWMSlayer);

// you can do it both ways:
// 1. now move the markers to the top of the stack
var yourMarkers = map.getLayersByName("the marker layer name")[0];
map.setLayerIndex(yourMarkers, map.layers.length-1);

// 2. OR you move the WMS down
// this is more succint
map.setLayerIndex(yourWMSLayer, map.layers.length-2);

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.