Google Maps API v3 infowindow关闭事件/回调?

Col*_*lin 71 javascript jquery google-maps google-maps-api-3

我喜欢跟踪我的谷歌地图界面上打开的任何和所有信息窗口(我将他们的名字存储在一个数组中),但我无法弄清楚如何通过"x"关闭它们从阵列中删除它们"在每个人的右上角.

我可以听一些回调吗?或者也许我可以做点什么 addListener("close", infowindow1, etc

Jor*_*rge 160

有一个closeclick可以帮助你的infowindows电话活动

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
});
Run Code Online (Sandbox Code Playgroud)

  • 请注意,当单击附近的另一个标记并且该标记自动关闭时,这不会触发...例如,当发生滚动以确保信息窗口位于地图的可见边界时. (3认同)

Ben*_*ild 12

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

这样做的原因是单击另一个元素会导致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);
Run Code Online (Sandbox Code Playgroud)

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