Check if infowindow is opened Google Maps v3

雨燕双飞 提交于 2019-11-30 04:18:58
doublesharp

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

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;
}

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

infowindow.getMap() returns null if infowindow is closed. So you can use simply

if (infowindow.getMap());

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!