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 – using eventListeners


Let's demonstrate how to use event listeners. We'll start off with a simple example that will send an alert anytime the map finishes zooming.

  1. The first thing we'll need to do is to create a function that will get called by the event listener when some event happens. We'll have to do this for both the eventListeners method and the map.events.register method which we'll talk about shortly. Let's create a simple function that will send an alert when the user zooms in. Place this as the first line in your init() function, before your OpenLayers map code:

    function alert_on_zoom(event){ alert('You finished zooming'); }
  2. Next, we'll need to create our map object and pass in an eventListeners object. We'll want to listen for a zoomend event type, which is an event that happens when the map has finished zooming. So, the key inside our eventListeners object will be the event type, which is 'zoomend'. The value will be the name of the function we wish to get called...