[OpenLayers-Users] registering and unregistering map events

Puneet Kishor punk.kish at gmail.com
Mon Oct 3 19:44:43 EDT 2011


I would welcome suggestions to solve the following. I want to "enable" the "activation" of a functionality at a certain zoom level, and after the functionality is completed, I want the "deactivate" the functionality, but still keep it enable-able (huh!). See below for a better description of the problem --


    // add a "zoomend" event so…
    map.events.register("zoomend", map, function() {
    
        // when the map is zoomed in at the correct level,
        if (map.getZoom() > 3) {
            
            // a button is activated so that when the button is clicked…
            $("#button").bind("click", function() {
                
                // the button is turned "on"
                ..
                
                // the map begins listening for clicks to do 
                // cool functionality
                map.events.register("click", map, function(e) {
                    coolFunctionality(e);
                })
            });

        }
        
        // but when the map zoom is not at the correct level,
        else {
        
            // map is no longer listening to clicks
            map.events.unregister("click", map, function(e) {
                coolFunctionality(e);
            })
                
            // and the button is turned "off" and deactivated
            $("#button").unbind("click", function() {});   
                
        }
    });
    
In another part of the script

    "coolFunctionality": function (e) {
        
        // do cool functionality, 
        ..
        
        // turn the button "off" (but it can still be clicked)
        ..
        
        // and turn off the map listener so it may not be fired accidentally
        map.events.unregister("click", map, function(e) {
            coolFunctionality(e);
        })
    }
    
So, the problem is, when I am in `coolFunctionality()` and `unregister` the event, I am still in the zoom level range where the event is `register`ed. Hmmmm... what to do?

--
Puneet Kishor


More information about the Users mailing list