Creating a custom control
OpenLayers has plenty of controls and interactions that address a broad range of needs. However, the requirements that we could have to build a new web application inevitably demand the creation of a new one or the extension of an existing one.
OpenLayers 2 came with a layer switcher control that's not made it to OpenLayers 3. This is not a problem, as for this recipe, we're going to create our own layer switcher control. Apart from enabling the user to switch between a list of layers, this control will demonstrate how to extend the base control class (ol.control.Control
) to utilize some default control behavior and then build on top of the base control to deliver an interactive control the user can click or tap to change the layer.
The source code for this recipe can be found in ch07/ch07-custom-control
, and here's a preview of our custom control placed at the bottom-left of the map:
How to do it…
Create an HTML file with the OpenLayers dependencies and a
div
element...