46

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

Fred Vicentin
  • 967
  • 3
  • 16
  • 33
  • 1
    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: https://developers.google.com/maps/documentation/javascript/reference/info-window#InfoWindowOptions.content Perhaps it depends on how you set the content (string or node). – Max Waterman Aug 13 '20 at 02:09

5 Answers5

67

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");
}
prokki
  • 111
  • 3
  • 12
doublesharp
  • 26,888
  • 6
  • 52
  • 73
  • The infoWindow class does not have a method called .getMap(). – Marcelo Sep 13 '12 at 16:37
  • 1
    In the documentation (https://developers.google.com/maps/documentation/javascript/reference#InfoWindow) it is not listed, but it works for me. You can see a screenshot of it listed in the Chrome console here: http://imgur.com/0byQx – doublesharp Sep 13 '12 at 17:07
  • This Google Groups post has the same suggestion: https://groups.google.com/forum/#!msg/google-maps-js-api-v3/_L0RdmgNCbo/IB2MlW5iwpgJ – doublesharp Sep 13 '12 at 17:14
  • 2
    The thing about undocumented features is that they may change, or vanish altogether, without warning in a future update. I would advise against using them, even if they seem to work here and now. – Marcelo Sep 13 '12 at 17:18
  • Completely agree, however the alternate answer to the above question is "you can't" so I figured it was better than nothing. I will edit to to indicate that it is an undocumented feature. – doublesharp Sep 13 '12 at 17:22
  • I tried to use the call the function Initialize(); and put to call on the final of initialize isInfoWindowOpen(infoWindow);, then, I had put the if(isInfoWindowOpen)...etc, but it dont worked, I tested with alerts, when I call the function nothing work...=/ – Fred Vicentin Sep 13 '12 at 18:30
  • 2
    Can you post the full code? I tested with some of my own implementations and it **does** work, it just isn't officially supported as @Marcelo noted. You don't need to have the function defined as I did, assuming the variable is called `infoWindow` you can also write it as: `if (infoWindow.getMap()) { window.alert('visible'); } else { window.alert('hidden'); }` – doublesharp Sep 13 '12 at 18:49
  • The function in the post is the wrong way round - returns false when infowindow is open. – Steve Chambers Oct 22 '13 at 11:21
  • Thanks for that, i don't know why you ask for `"undefined"`; but i use `getMap() != null` to know if it's opened or closed. Here's my my [solution](http://stackoverflow.com/a/24657050/2835520) – IgniteCoders Jul 09 '14 at 16:11
  • This is good if you only have to deal with one marker. If you have multiple markers that each update the InfoWindow object on click and you want to implement a toggle (click on marker to open, click on it again to close), then this won't work. – thdoan Mar 10 '16 at 10:52
26

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;
}
ronalchn
  • 12,225
  • 10
  • 51
  • 61
pedro
  • 261
  • 2
  • 2
  • Thanks. That's better than use undocumented google's function – irriss Dec 16 '12 at 06:19
  • I definitely prefer this approach. You'll likely have some form of "marker manager" anyway, so implementing this is easy. – Eric Apr 24 '13 at 22:37
  • 1
    Clear and fast solution, working and after almost 7 years. Thanks – Angel T Apr 06 '17 at 06:49
  • One small issue, the user can close the info window by clicking "x", hence the "opened" state will be out of sync. – mike.void Nov 18 '21 at 18:35
  • Adding to my comment, you can fix the mentioned issue by using the "closeclick" event https://developers.google.com/maps/documentation/javascript/reference/info-window#InfoWindow.closeclick – mike.void Nov 19 '21 at 12:43
12

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...

John Sully
  • 281
  • 4
  • 9
3

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

So you can use simply:

if (infowindow.getMap());
SHR
  • 7,940
  • 9
  • 38
  • 57
3

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);
        }
    }
);
Filip Seman
  • 1,252
  • 2
  • 15
  • 22
  • best and easiest solution of all to this day, no need to hack or modify/add functionality, just using the one that is provided. Wondering if the set method was available when OP asked the question! Dakujem velmi pekne – hocikto Jan 25 '21 at 08:25
  • I just stumbled across the similar need, and saw this answer. Love it. Very straightforward and lightweight! Saved me the need to bloat my code with an extra function. – Eric Liang Mar 08 '21 at 20:03