[GIS] How to see if a function is already registered to an event on an OpenLayers map

eventsopenlayers-2

I am creating a data editing map using OpenLayers. I have two main states that the map editor can be in – editing existing features, or adding new ones. When a user clicks the "Edit Features" button, the editOnClick event is registered to the map. When the user clicks the "Add Feature" button, the drawFeatureOnClick event is registered to the map. I want to include a simple if statement to prevent OpenLayers from registering the same event multiple times, as well as remove the other button's event if it is active. Here's some pseudocode for the edit button:

$("#editfeatures").button({text: "Edit Features"}).click(function() {
    if (<editOnClick not already registered>) {
        map.events.register("click", map, editOnClick);
    }
    if (<drawFeatureOnClick currently registered>) {
        map.events.unregister("click", map, drawFeatureOnClick);
    }
}

I tried inspecting the map object using the Chrome debug tools, but I couldn't find an obvious way to tell if an event is currently registered on the map. How can I see what event listeners are registered on the map?

Best Answer

As an example, go to http://www.openlayers.org/dev/examples/click.html

Type in your console map.events.listeners to get a list of registered events classified by events types.

To see if you really added your event to the map, just count the type of events you registered.

A sample could be

console.log('Before adding a new click event', map.events.listeners.click.length);

function test() {
  console.log('ok');
}

map.events.register("click", map, test);

console.log('After adding a new click event', map.events.listeners.click.length);

You will the number go from 9 to 10 registered click events on the map