Search code examples
openlayers

Force layer to top


Is there any way to force a layer to always be on top regardless of if other layers are added to the map? I've tried setting the layer index of the layer to something very high (e.g. 1000), but it appears that if another layer is added later it automatically gets put on top.


Solution

  • I had to write a new Layer class to replace WFS.js that was deprecated in OL 2.12. There are markers in this class that always need to be on top (higher z-index) than the other layers. Here is a function of this class:

    moveMarkersToTop : function() 
    {
       if (this.markers) {  // markers are of type OpenLayers.Layer.Markers
          this.map.setLayerIndex(this.markers, this.map.getNumLayers());
       }
    },
    

    You can register a function like this when an "addlayer" event is triggered from the OpenLayers.Map class, for example.