Google Maps API v3 infowindow close event/callback?
Solution 1:
there's an event for infowindows call closeclick
that can help you
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
});
Solution 2:
The only consistent solution I've found here is to retain a pointer to the infoWindow
and check its .getMap()
method whenever you need to validate whether it has been closed.
The reason for this is that clicking another element can cause the infoWindow to be dismissed for other reasons... without the closeclick
event firing.
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 infoWindow
was closed using the "X" button, then monitoring closeclick
is fine. However, there are other reasons it may be or have been closed.
Solution 3:
infoWindow.addListener('closeclick', ()=>{
// Handle focus manually.
});