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
});
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);
通过简化和扩展 最赞成的解决方案,您可以在处理标记 click 事件期间创建标记,同时可以将由于 x 图标的 closeclick事件而删除的标记打包。
这里的一个例子包括通过在标记上添加布尔 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);
});
}
});