Book Image

OpenLayers 3.x Cookbook - Second Edition

By : Peter J. Langley, Antonio Santiago Perez
Book Image

OpenLayers 3.x Cookbook - Second Edition

By: Peter J. Langley, Antonio Santiago Perez

Overview of this book

OpenLayers 3 is one of the most important and complete open source JavaScript mapping libraries today. Throughout this book, you will go through recipes that expose various features of OpenLayers 3, allowing you to gain an insight into building complex GIS web applications. You will get to grips with the basics of creating a map with common functionality and quickly advance to more complicated solutions that address modern challenges. You will explore into maps, raster and vector layers, and styling in depth. This book also includes problem solving and how-to recipes for the most common and important tasks.
Table of Contents (14 chapters)
OpenLayers 3.x Cookbook Second Edition
About the Authors
About the Reviewer

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…

  1. Create an HTML file with the OpenLayers dependencies and a div element...