Book Image

OpenLayers 2.10 Beginner's Guide

Book Image

OpenLayers 2.10 Beginner's Guide

Overview of this book

Table of Contents (18 chapters)
OpenLayers 2.10
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for Action – creating a custom TYPE_TOGGLE control


We're going to create a TYPE_TOGGLE control button. We'll actually be using events in two ways. We'll use events to call a function when the control is activated/deactivated (i.e., toggled). This activated/deactivated concept is important, as we cannot use controls if they are not activated.

When the toggle control is activated, the activate() function that gets called will add an event handler to the map. The map event handler will look for a click event and upon a click will call a function which updates the base layer's opacity. When the control is deactivated, the deactivate() function that gets called will remove the map event handler.

  1. Open up the previous example. We'll be using that as the basis for this example. We don't really need to change anything there, so let's go ahead and start creating our buttons!

  2. First, we're going to create the function that will randomly change the map's base layer's opacity when the map is clicked...