views:

40

answers:

3

somewhat simple problem(to explain) this time:

i have an array of markers that i equip with eventlisteners thusly:

for (i in markersArray) {
google.maps.event.addListener(markersArray[i], 'click', function() {        
    //stuff it does
    google.maps.event.removeListener(?????)     //remove self... but HOW?!
});}

as i mention in the comment, i simply want the listener to be removed once it is clicked.

problem is that i dont know what the handle for the listener is.

A: 

I'm quite sure in this case you would use an array of listeners (or an object that encapsulated markers AND listeners). google.maps.event returns an event object. Check the documentation.

var markersListeners = [];

for (i in markersArray)
{
    markersListeners[i] = google.maps.event.addListener(markersArray[i], 'click', function()
    {
        //stuff it does
        google.maps.event.removeListener(markersListeners[i]);
    });
}

Disclaimer: I haven't checked the syntax. You might also try removeListener(markersArray[i]) as I've seen it done, but do not know if it works.

Eric Muyser
removeListener(markersArray[i]) did not work for me when i tried yesterday, kept giving me api errors
Stjerneklar
A: 

You can also use clearListeners(instance:Object, eventName:string)http://code.google.com/apis/maps/documentation/javascript/reference.html

Marimuthu Madasamy
+1  A: 

You can use the "addListenerOnce". Then you don't even have to bother with removing the listener.

addListenerOnce(instance:Object, eventName:string, handler:Function)

Like event.AddListener, but the handler removes itself after handling the first event.

CrazyEnigma
nice one, just what i needed :)
Stjerneklar