Google Maps API v3:如何删除事件侦听器?

时间:2022-11-23 08:13:01

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

如何在Google Maps API v3中删除'bounds_changed'事件侦听器?

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

4 个解决方案

#1


139  

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

通常,您可以在Google Maps API文档中找到此类问题的答案。

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.

正如Andrew所说,addListener返回一个句柄,稍后您可以使用它来删除监听器。这是因为单个事件可以有许多侦听器并且要删除它们,您必须保存对每个连接侦听器的引用。

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.

这是Google Maps API参考,您可以在其中阅读相关信息。

#2


84  

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

addListener返回一个句柄,稍后您可以将其传递给removeListener:

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

google.maps.event.removeListener(listenerHandle);

#3


14  

This seems to work in the current release.

这似乎适用于当前版本。

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

#4


0  

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

如果你不能以某种方式保持监听器对象,你可以直接删除监听器google.maps.event.clearListeners(objectListened,'event');

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

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

#1


139  

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

通常,您可以在Google Maps API文档中找到此类问题的答案。

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.

正如Andrew所说,addListener返回一个句柄,稍后您可以使用它来删除监听器。这是因为单个事件可以有许多侦听器并且要删除它们,您必须保存对每个连接侦听器的引用。

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.

这是Google Maps API参考,您可以在其中阅读相关信息。

#2


84  

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

addListener返回一个句柄,稍后您可以将其传递给removeListener:

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

google.maps.event.removeListener(listenerHandle);

#3


14  

This seems to work in the current release.

这似乎适用于当前版本。

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

#4


0  

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

如果你不能以某种方式保持监听器对象,你可以直接删除监听器google.maps.event.clearListeners(objectListened,'event');

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

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