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.
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 themap.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 yourinit()
function, before your OpenLayers map code:function alert_on_zoom(event){ alert('You finished zooming'); }
Next, we'll need to create our map object and pass in an
eventListeners
object. We'll want to listen for azoomend
event type, which is an event that happens when the map has finished zooming. So, the key inside oureventListeners
object will be the event type, which is'zoomend'
. The value will be the name of the function we wish to get called...