检查infowindow是否已打开Goog​​le Maps v3

Fre*_*tin 33 infowindow google-maps-api-3

拜托,我需要帮助.

我想检查一下我的信息窗是否打开了.

例如:

if (infowindow.isOpened)
{
   doSomething()
}
Run Code Online (Sandbox Code Playgroud)

要么

if (infowindow.close)
{
   doAnotherthing();
}
Run Code Online (Sandbox Code Playgroud)

我不知道怎么做

dou*_*arp 59

这是一个未记录的功能,因此可能会在不事先通知的情况下进行更改,但是该infoWindow.close()方法会将对象上的地图设置为null(这就是为什么infoWindow.open(map, [anchor])需要传入a Map),因此您可以检查此属性以判断它当前是否存在显示:

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
}
Run Code Online (Sandbox Code Playgroud)

更新: 另一种可能有用的方法是将此isOpen()方法添加到InfoWindow prototype.

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

  • 关于未记录功能的事情是它们可能会更改或完全消失,而不会在未来的更新中发出警告。我建议不要使用它们,即使它们似乎在这里和现在都有效。 (2认同)
  • 你可以发布完整的代码吗?我测试了一些我自己的实现,****确实**工作,它只是没有正式支持,因为@Marcelo注意到.您不需要像我一样定义函数,假设变量名为`infoWindow`,您也可以将其写为:`if(infoWindow.getMap()){window.alert('visible'); } else {window.alert('hidden'); }` (2认同)

小智 23

直到谷歌没有给我们任何更好的方法,你可以添加属性到infoWindow对象.就像是:

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;
}
Run Code Online (Sandbox Code Playgroud)


Joh*_*lly 12

我修改了google.maps.InfoWindow的原型并更改了open/close以设置/清除属性:

//
// 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;
    };
Run Code Online (Sandbox Code Playgroud)

您还需要监视closeclick事件,因为单击关闭按钮不会调用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);
Run Code Online (Sandbox Code Playgroud)

调用InfoWindow.getOpenedState()返回一个布尔值,它反映了infowindow的状态(打开/关闭).

我选择这样做而不是使用InfoWindow.getMap()MVCObject.get('map')方法,因为众所周知的使用无证行为的陷阱.但谷歌使用MVCObject.set('map', null)强制从DOM中删除InfoWindow,所以这不太可能改变......