Map is the core concept in OpenLayers. It allows us to visualize information from different kinds of layers and brings us methods to manage layers attached to it.
In this recipe, we will learn how to control layers. This is important because add, remove, or reorder layers are very common operations we need to do on almost every web mapping application.
The application will show a map on the left and a control panel on the right, with some buttons to control the layers.
There is not much to say about the HTML code for the layout. We have used a table to put the map on the left and the set of buttons on the right. In addition, we have associated actions to the buttons that will be executed when they are clicked.
With respect to the OpenLayers code, we have created the map instance working in the allOverlays
mode. This will let us move any layer without being worried about a base layer:
Later, we created three WMS layers and added them to the map. For some of them we have set the opacity
property to 50%
to see through them:
It is very important to note that we have used the same name for the option's value attribute in the HTML select element as we have used for the layer. Later, this will let us select a map's layer by its name.
Next, we have added an OpenLayers.Control.LayerSwitcher
control by setting its ascending
property to false
:
You can think of the map as storing layers in a stack and they are rendered from bottom to top, so the above layers can hide beneath the below layers depending on its opacity and extent.
Tip
By default the
ascending
property is true
, and the layer switcher control shows the layers of the map in the reverse order, that is, the bottom layer is drawn first in the control and the top layer is drawn last. You can avoid this by setting ascending
to false
.
Finally, the only thing we need to take a look at is the code responsible for button actions, which is the most interesting code in this recipe.
Let's take a look to the raiseLayer()
action (which is very similar to lowerLayer()
action):
First, we get the name of the currently selected layer in the select element (don't worry if you don't understand that line completely, it is more related to the Dojo framework than to OpenLayers).
Then, we use the
map.getLayersByName()
method, which returns an array with all the layers that have the specified name. Because of this, we get the first element of the array.
Now we have a reference to the layer instance. We can raise it in the map using the map.raiseLayer()
method. You can raise it by one or more positions indicating a delta
number or, like in the lowerLayer()
function, you can lower it by one or more positions indicating a negative value.
Internally OpenLayers.Map
stores layers in an array (the layers
attribute) and they are rendered in the order they are stored in the array (so the first element is the bottom layer).
The topLayer()
and bottomLayer()
actions are similar too, they move the specified layer to the top or bottom of the stack. They both work using the map.setLayerIndex()
method, which is responsible to move a layer to a specified position.
Note
The method map.setLayerIndex()
is used internally by map.raiseLayer()
to move layers.
Because the bottom layer corresponds to the first layer in the array of layers, the bottomLayer()
action is the easiest to implement because we simply need to move the layer to the first position:
For the topLayer()
actions, we need to move the layer to the last position. To do this, we can get help from the map.getNumLayers()
method, which returns the total number of layers in the map. In this way, if we have four layers in the map, the last corresponds to the index 3
(because the index value changes from 0
to 3
).