43
votes

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

5
Why not just check the infoWindow's content has a parentElement? InfoWindow has a documented 'content' property, so you just need to check if it has a parentElement, or not: developers.google.com/maps/documentation/javascript/reference/… Perhaps it depends on how you set the content (string or node).Max Waterman

5 Answers

65
votes

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");
}
26
votes

Until google doesn't give us any better way of doing this, you can add a property to the infoWindow objects. Something like:

google.maps.InfoWindow.prototype.opened = false;
infoWindow = new google.maps.InfoWindow({content: '<h1> Olá mundo </h1>'});

if(infoWindow.opened){
   // do something
   infoWindow.opened = false;
}
else{
   // do something else
   infoWindow.opened = true;
}
12
votes

I modified the prototype for google.maps.InfoWindow and changed open/close to set/clear a property:

//
// modify the prototype for google.maps.Infowindow so that it is capable of tracking
// the opened state of the window.  we track the state via boolean which is set when
// open() or close() are called.  in addition to these, the closeclick event is
// monitored so that the value of _openedState can be set when the close button is
// clicked (see code at bottom of this file).
//
google.maps.InfoWindow.prototype._open = google.maps.InfoWindow.prototype.open;
google.maps.InfoWindow.prototype._close = google.maps.InfoWindow.prototype.close;
google.maps.InfoWindow.prototype._openedState = false;

google.maps.InfoWindow.prototype.open =
    function (map, anchor) {
        this._openedState = true;
        this._open(map, anchor);
    };

google.maps.InfoWindow.prototype.close =
    function () {
        this._openedState = false;
        this._close();
    };

google.maps.InfoWindow.prototype.getOpenedState =
    function () {
        return this._openedState;
    };

google.maps.InfoWindow.prototype.setOpenedState =
    function (val) {
        this._openedState = val;
    };

You also need to monitor the closeclick event because clicking on the close button does not call close().

//
// monitor the closelick event and set opened state false when the close
// button is clicked.
//
(function (w) {
    google.maps.event.addListener(w, "closeclick", function (e) {
        w.setOpenedState(false);
    });
})(infowindow);

Calling InfoWindow.getOpenedState() returns a boolean which reflects the state (opened/closed) of the infowindow.

I chose to do it this way instead of the using the InfoWindow.getMap() or MVCObject.get('map') method because of the well known pitfalls of using undocumented behavior. However google uses MVCObject.set('map', null) to force the removal of the InfoWindow from the DOM, so it is unlikely that this will change...

3
votes

infowindow.getMap() returns null if infowindow is closed.

So you can use simply:

if (infowindow.getMap());
2
votes

You can simply set key and value for infoWindow: infoWindow.set('closed', true);

example:

const infoWindow = new google.maps.InfoWindow({
    content: 'foo',
    position: {
        lat: some_number,
        lng: some_number
    }
});

infoWindow.set('closed', true);

// Clicking on polyline for this example
// Can be marker too
polyline.addListener(
    'click',
    () => {
        if (infoWindow.get('closed')) {
            infoWindow.open(map);
            infoWindow.set('closed', false);
        } else {
            infoWindow.close();
            infoWindow.set('closed', true);
        }
    }
);