简体   繁体   English

如何检查google maps api v3中的标记是否已存在事件

[英]How to check if event already exists for marker in google maps api v3

I have created a marker and attached click event listener to the marker. 我已经为标记创建了一个标记和附加的单击事件监听器。 However, I would like to check if the click event has been already attached to the marker and if not, attach the click event listener. 但是,我想检查click事件是否已经附加到标记,如果没有,请附加click事件监听器。

// If no click event listener, then attach the listener
google.maps.event.addListener(marker, 'click', function() {
  infowindow.open(map, marker);
});

I can however check with a custom flag in the marker object as: 但是,我可以使用标记对象中的自定义标志进行检查:

// If no click event listener, then attach the listener
if (! marker._isClickEventBound) {
  google.maps.event.addListener(marker, 'click', function() {
    infowindow.open(map, marker);
    marker._isClickEventBound = true;
  });
}

The same code is executed during the addition of new marker and editing the marker. 在添加新标记和编辑标记期间执行相同的代码。 I just wanted to know if there is any other way rather than adding a flag ? 我只是想知道是否有其他方式而不是添加标志?

Adding a flag is actually fine. 添加标志实际上很好。

Apart from that, google.maps.event.addListener returns you an event object. 除此之外, google.maps.event.addListener还会返回一个事件对象。 You can keep track of all event objects you've added to also clean up marker events if needed... 您可以跟踪已添加的所有事件对象,以便在需要时清理标记事件...

hasListeners returns a boolean: hasListeners返回一个布尔值:

google.maps.event.hasListeners(marker,'click')

also works on the map itself: 也适用于地图本身:

google.maps.event.hasListeners(map,'idle')

丑陋的黑客将删除所有听众并再次附加它。

google.maps.event.clearListeners(map, 'click');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM