【问题标题】:Google Maps API v3 infowindow close event/callback?Google Maps API v3 infowindow 关闭事件/回调?
【发布时间】:2011-10-10 07:13:05
【问题描述】:

我喜欢跟踪在我的 Google 地图界面上打开的所有信息窗口(我将它们的名称存储在一个数组中),但我不知道如何在它们关闭时从我的数组中删除它们每一个右上角的“x”。

有什么我可以收听的回调吗?或者也许我可以做类似的事情 addListener("close", infowindow1, etc ?

【问题讨论】:

    标签: javascript jquery google-maps google-maps-api-3


    【解决方案1】:

    有一个 infowindows 事件调用 closeclick 可以帮助您

    var currentMark;
    var infoWindow = new google.maps.InfoWindow({
                content: 'im an info windows'
            });
    google.maps.event.addListener(marker, 'click', function () {
              infoWindow.open(map, this);
              currentMark = this;
    
    });
    google.maps.event.addListener(infoWindow,'closeclick',function(){
       currentMark.setMap(null); //removes the marker
       // then, remove the infowindows name from the array
    });
    

    【讨论】:

    • 请注意,当单击附近的另一个标记并且该标记自动关闭时不会触发...例如当滚动以确保信息窗口位于地图的可见范围内时.
    【解决方案2】:

    我在这里找到的唯一一致的解决方案是保留指向 infoWindow 的指针,并在需要验证它是否已关闭时检查其 .getMap() 方法。

    这样做的原因是单击另一个元素可能会导致 infoWindow 因其他原因而被关闭...而不会触发 closeclick 事件。

    var infoWindow = new google.maps.InfoWindow({ content: 'Something to put here.' });
    infoWindow.open(map, infoWindow);
    
    setInterval(function ()
    {
        console.log("infoWindow is bound to map: "+(infoWindow.getMap() ? true : false));
    
    }, 1000);
    

    ...如果您真的只关心infoWindow 是否使用“X”按钮关闭,那么监控closeclick 就可以了。但是,它可能或已经关闭还有其他原因。

    【讨论】:

      【解决方案3】:

      试试这个:

      var closeBtn = $('.gm-style-iw').next();
      closeBtn.click(function(){
          //other things you want to do when close btn is click
          that.infowindow.close();
      });
      

      我覆盖了这个点击函数,因为在我改变它的 css/位置后,点击按钮在 safari 中不起作用。

      【讨论】:

        【解决方案4】:

        most upvoted solution进行简化和扩展,可以在处理marker点击事件的过程中创建marker,同时让你打包删除由于x图标的closeclick事件。

        这是一个示例,其中包括通过在标记上添加布尔 hasInfoWindow 状态来避免重复信息窗口创建。

          newMarker.addListener('click', function () {
            // If a marker does not yet have an info window, create and show it
            if (newMarker['hasInfoWindow'] !== true) {
              newInfoWindow = new google.maps.InfoWindow({content: infoContent}); 
              mapSet['infoWindowsObj'].push(newInfoWindow);
              newMarker['hasInfoWindow'] = true;
              newInfoWindow.open(mapSet, newMarker);
        
              // If info window is dismissed individually, fully remove object
              google.maps.event.addListener(newInfoWindow, 'closeclick', function () {
                newInfoWindow.setMap(null);
                newMarker['hasInfoWindow'] = false;
                mapSet['infoWindowsObj'].filter(arrayItem => arrayItem !== newInfoWindow);
              });
            }
          });
        

        然后,如果您想删除由于地图上的点击事件而打开的所有信息窗口,您可以遍历 mapSet['infoWindowsObj'] 的内容以完全删除每个。

        我相信这种行为可以让您在大多数情况下无需使用 infowindow,而无需按照谷歌的 custom popup example 重新实现整个事情。

        【讨论】:

          【解决方案5】:

          这很简单找到关闭信息窗口按钮的类,即'.gm-ui-hover-effect'

          触发关闭信息窗口

          $('.gm-ui-hover-effect').trigger('click');

          【讨论】:

            【解决方案6】:
            infoWindow.addListener('closeclick', ()=>{
              // Handle focus manually.
            });
            

            【讨论】:

              猜你喜欢
              • 2012-01-06
              • 2011-01-03
              • 1970-01-01
              • 1970-01-01
              • 2011-02-11
              • 2012-08-15
              • 2011-11-06
              • 2016-02-10
              • 2010-12-06
              相关资源
              最近更新 更多