Javascript Google Maps API v3 信息窗口关闭事件/回调?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6777721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:09:06  来源:igfitidea点击:

Google Maps API v3 infowindow close event/callback?

javascriptjquerygoogle-mapsgoogle-maps-api-3

提问by Colin

I like to keep track of any and all infowindows that are open on my Google Maps interface (I store their names in an array), but I can't figure out how to remove them from my array when they are closed via the "x" in the upper right hand corner of each one.

我喜欢跟踪在我的 Google 地图界面上打开的所有信息窗口(我将它们的名称存储在一个数组中),但是当它们通过“x”关闭时,我无法弄清楚如何从我的数组中删除它们"在每一个的右上角。

Is there some sort of callback I can listen for? Or maybe I can do something like addListener("close", infowindow1, etc?

有什么我可以听的回调吗?或者也许我可以做类似的事情 addListener("close", infowindow1, etc

采纳答案by Ben Guild

The only consistent solution I've found here is to retain a pointer to the infoWindowand check its .getMap()method whenever you need to validate whether it has been closed.

我在这里找到的唯一一致的解决方案是保留一个指向 的指针,infoWindow.getMap()在需要验证它是否已关闭时检查其方法。

The reason for this is that clicking another element can cause the infoWindow to be dismissed for other reasons... without the closeclickevent firing.

这样做的原因是单击另一个元素可能会导致 infoWindow 由于其他原因而被关闭……而不会closeclick触发事件。

var infoWindow = new google.maps.InfoWindow({ content: 'Something to put here.' });
infoWindow.open(map, infoWindow);

setInterval(function ()
{
    console.log("infoWindow is bound to map: "+(infoWindow.getMap() ? true : false));

}, 1000);

... If you literally only care if the infoWindowwas closed using the "X" button, then monitoring closeclickis fine. However, there are other reasons it may be or have been closed.

...如果你真的只关心是否infoWindow使用“X”按钮关闭,那么监控closeclick就可以了。但是,它可能或已经关闭还有其他原因。

回答by Jorge

there's an event for infowindows call closeclickthat can help you

有一个 infowindows 呼叫事件closeclick可以帮助你

var currentMark;
var infoWindow = new google.maps.InfoWindow({
            content: 'im an info windows'
        });
google.maps.event.addListener(marker, 'click', function () {
          infoWindow.open(map, this);
          currentMark = this;

});
google.maps.event.addListener(infoWindow,'closeclick',function(){
   currentMark.setMap(null); //removes the marker
   // then, remove the infowindows name from the array
});

回答by Tina Chen

Try this:

尝试这个:

var closeBtn = $('.gm-style-iw').next();
closeBtn.click(function(){
    //other things you want to do when close btn is click
    that.infowindow.close();
});

I overwrite this click function because the click button won't work in safari after I change the css/position of it.

我覆盖了这个点击功能,因为在我更改它的 css/位置后,点击按钮在 safari 中不起作用。

回答by Rob

Simplifying and extending the most upvoted solution, you can create the marker during the handling of the marker click event, letting you package its removal due to the x icon's closeclickevent at the same time.

简化和扩展最受好评的解决方案,您可以在处理标记单击事件期间创建标记,让您同时打包由于 x 图标的closeclick事件而导致的删除。

Here's an example that includes duplicate info window creation avoidance by tacking a boolean hasInfoWindowstatus on markers.

这是一个示例,其中包括通过hasInfoWindow在标记上添加布尔状态来避免创建重复信息窗口。

  newMarker.addListener('click', function () {
    // If a marker does not yet have an info window, create and show it
    if (newMarker['hasInfoWindow'] !== true) {
      newInfoWindow = new google.maps.InfoWindow({content: infoContent}); 
      mapSet['infoWindowsObj'].push(newInfoWindow);
      newMarker['hasInfoWindow'] = true;
      newInfoWindow.open(mapSet, newMarker);

      // If info window is dismissed individually, fully remove object
      google.maps.event.addListener(newInfoWindow, 'closeclick', function () {
        newInfoWindow.setMap(null);
        newMarker['hasInfoWindow'] = false;
        mapSet['infoWindowsObj'].filter(arrayItem => arrayItem !== newInfoWindow);
      });
    }
  });

Then if you want to remove all open info windows due to a click event on a map, you can iterate through the contents of mapSet['infoWindowsObj']to fully remove each.

然后,如果您想删除由于地图上的单击事件而导致的所有打开的信息窗口,您可以遍历 的内容mapSet['infoWindowsObj']以完全删除每个窗口。

I believe this behavior lets you get away with using infowindow for most cases without having to reimplement the whole thing as per google's custom popup example.

我相信这种行为可以让您在大多数情况下使用 infowindow 而不必按照 google 的自定义弹出示例重新实现整个事情。