1

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?

2 Answers 2

3

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

1
  • Thanks for this. This didn't quite solve my problem of needing to check the existence of a specific function registered to a specific event, but it helped get me on the right track.
    – Tanner
    Commented Oct 31, 2013 at 19:15
2

I fixed my problem with a simple function:

function isEventRegistered(eventType, object, func) {
    var registered = false;
    for (var i = 0; i < object.events.listeners[eventType].length; i++) {
        eventObj = object.events.listeners[eventType][i];
        if (eventObj.func === func) {
            registered = true;
        }
    }
    return registered;
}

So now my edit button is this:

$("#editfeatures").button({text: "Edit Features"}).click(function() {
    if (!isEventRegistered('click', map, editOnClick)) {
        map.events.register('click', map, editOnClick);
    }
    if (isEventRegistered('click', map, drawFeatureOnClick)) {
        map.events.unregister('click', map, drawFeatureOnClick);
    }
});
1
  • Hi @Tanner. Very good answer! :) Please, allow me to suggest something. You only test the ´func´. Shouldn't you include a 4th parameter for the scope of the callback and test it too? I might be wrong but I think that the OpenLayers.Event is entirely defined by these 3: ´eventType´, ´obj´ (scope of func) and ´func´. What do you think?
    – joaorodr84
    Commented Jul 17, 2015 at 0:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.