Google Maps API v3: How to remove an Event Listener?

JavascriptGoogle MapsGoogle Maps-Api-3

Javascript Problem Overview


How do I remove the 'bounds_changed' Event listener in Google Maps API v3?

google.maps.event.removeListener(_???_);    

Javascript Solutions


Solution 1 - Javascript

Usually you can find answers to such questions in Google Maps API documentation.

As Andrew said, addListener returns a handle which you can use later to remove the listener. That's because a single event can have many listeners and to remove them you must save a reference to each of attached listeners.

There's also a function which removes all of the listeners at the same time:

clearListeners(instance:Object, eventName:string);
//In your case:
google.maps.event.clearListeners(map, 'bounds_changed');

Here's the Google Maps API reference where you can read about it.

Solution 2 - Javascript

addListener returns a handle which you can later pass to removeListener:

var listenerHandle = google.maps.event.addListener(map, 'bounds_changed', function() {

google.maps.event.removeListener(listenerHandle);

Solution 3 - Javascript

This seems to work in the current release.

var listenerHandle = google.maps.event.addListener(map, 'bounds_changed', function() {
    // Handler code.
});
listenerHandle.remove();

Solution 4 - Javascript

If you couldnt hold the listener object somehow you could remove listener(s) directly as google.maps.event.clearListeners(objectListened, 'event');

Ex: google.maps.event.clearListeners(map, 'bounds_changed');

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionmp_View Question on Stackoverflow
Solution 1 - JavascriptMaiku MoriView Answer on Stackoverflow
Solution 2 - JavascriptAndrewView Answer on Stackoverflow
Solution 3 - Javascriptether6View Answer on Stackoverflow
Solution 4 - JavascriptTarık Özgün GünerView Answer on Stackoverflow