Search code examples
google-maps-api-3infowindow

Check if infowindow is opened Google Maps v3


Please, I need a help.

I want to check if my infowindow is opened.

For example:

if (infowindow.isOpened)
{
   doSomething()
}

or

if (infowindow.close)
{
   doAnotherthing();
}

I dont have any idea, how to do this


Solution

  • This is an undocumented feature, and is therefore subject to change without notice, however the infoWindow.close() method sets the map on the object to null (this is why infoWindow.open(map, [anchor]) requires that you pass in a Map), so you can check this property to tell if it is currently being displayed:

    function isInfoWindowOpen(infoWindow){
        var map = infoWindow.getMap();
        return (map !== null && typeof map !== "undefined");
    }
    
    if (isInfoWindowOpen(infoWindow)){
        // do something if it is open
    } else {
        // do something if it is closed
    }
    

    Update: Another potentially useful way to write this is to add an isOpen() method to the InfoWindow prototype.

    google.maps.InfoWindow.prototype.isOpen = function(){
        var map = this.getMap();
        return (map !== null && typeof map !== "undefined");
    }