【问题标题】:Check if infowindow is opened Google Maps v3检查 infowindow 是否打开 Google Maps v3
【发布时间】:2012-09-06 18:22:04
【问题描述】:

拜托,我需要帮助。

我想检查我的信息窗口是否打开。

例如:

if (infowindow.isOpened)
{
   doSomething()
}

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

我不知道该怎么做

【问题讨论】:

标签: google-maps-api-3 infowindow


【解决方案1】:

您可以简单地为 infoWindow 设置 keyvalueinfoWindow.set('closed', true);

示例:

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

【讨论】:

  • 迄今为止最好和最简单的解决方案,无需破解或修改/添加功能,只需使用提供的功能即可。想知道当 OP 提出问题时 set 方法是否可用! Dakujem velmi pekne
  • 我只是偶然发现了类似的需求,并看到了这个答案。爱它。非常简单和轻量级!让我无需使用额外的函数来膨胀我的代码。
【解决方案2】:

如果infowindow 关闭,infowindow.getMap() 返回 null。

所以你可以简单地使用:

if (infowindow.getMap());

【讨论】:

    【解决方案3】:

    这是一个未记录的功能,因此如有更改,恕不另行通知,但是infoWindow.close() 方法将对象上的地图设置为null(这就是为什么infoWindow.open(map, [anchor]) 要求您传入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
    }
    

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

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

    【讨论】:

    • infoWindow 类没有名为 .getMap() 的方法。
    • 在文档 (developers.google.com/maps/documentation/javascript/…) 中没有列出它,但它对我有用。您可以在此处的 Chrome 控制台中看到它的屏幕截图:imgur.com/0byQx
    • 这篇 Google 网上论坛帖子有同样的建议:groups.google.com/forum/#!msg/google-maps-js-api-v3/_L0RdmgNCbo/…
    • 关于未记录功能的问题是,它们可能会更改或完全消失,而不会在未来的更新中发出警告。我建议不要使用它们,即使它们现在似乎可以工作。
    • 你能发布完整的代码吗?我用我自己的一些实现进行了测试,它确实可以工作,但正如@Marcelo 所说,它没有得到官方支持。你不需要像我一样定义函数,假设变量被称为infoWindow你也可以写成:if (infoWindow.getMap()) { window.alert('visible'); } else { window.alert('hidden'); }
    【解决方案4】:

    我修改了 google.maps.InfoWindow 的原型并更改了打开/关闭以设置/清除属性:

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

    您还需要监视 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);
    

    调用InfoWindow.getOpenedState() 返回一个反映信息窗口状态(打开/关闭)的布尔值。

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

    【讨论】:

      【解决方案5】:

      在 google 没有为我们提供更好的方法之前,您可以将属性添加到 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;
      }
      

      【讨论】:

      • 谢谢。这比使用无证谷歌的功能要好
      • 我绝对喜欢这种方法。无论如何,您可能会有某种形式的“标记管理器”,因此实现它很容易。
      • 清晰快速的解决方案,工作了近 7 年。谢谢
      • 一个小问题,用户可以通过单击“x”关闭信息窗口,因此“打开”状态将不同步。
      • 添加到我的评论中,您可以通过使用“closeclick”事件developers.google.com/maps/documentation/javascript/reference/… 解决上述问题
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-24
      • 1970-01-01
      • 2011-02-11
      • 2011-06-21
      • 2011-12-09
      • 1970-01-01
      • 2012-01-06
      相关资源
      最近更新 更多