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

前端 未结 4 1854
逝去的感伤
逝去的感伤 2020-12-04 14:57

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

google.maps.event.removeListener(_???_);    
相关标签:
4条回答
  • 2020-12-04 15:31

    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');

    0 讨论(0)
  • 2020-12-04 15:32

    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.

    0 讨论(0)
  • 2020-12-04 15:39

    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);
    
    0 讨论(0)
  • 2020-12-04 15:49

    This seems to work in the current release.

    var listenerHandle = google.maps.event.addListener(map, 'bounds_changed', function() {
        // Handler code.
    });
    listenerHandle.remove();
    
    0 讨论(0)
提交回复
热议问题