【问题标题】:Close first infowindow when second is appear [duplicate]当第二个出现时关闭第一个信息窗口[重复]
【发布时间】:2014-06-29 09:43:56
【问题描述】:
function showpopup(lat, lng, info)
    {

        // Create infoWindow
        var infoWindow = new google.maps.InfoWindow({
            content: info
        });

    // Create marker
        var marker = new google.maps.Marker({
            position: new google.maps.LatLng(lat, lng),
            title: 'Lima',
            map: map.map,
            infoWindow: infoWindow
        });

    // This opens the infoWindow
        infoWindow.open(map, marker);
    }

我有这个函数,它采用三个参数并在地图上创建信息窗口,但现在我希望当用户打开第二个信息窗口时,第一个信息窗口使用 javascript 和 Gmap V3 关闭

【问题讨论】:

    标签: javascript html google-maps google-maps-api-3 infowindow


    【解决方案1】:

    您可以有一个全局变量来跟踪信息窗口并在使用close() 方法打开新窗口之前将其关闭,类似于

    var infoWindow; //global tracker
    function showpopup(lat, lng, info)
    {
     //close info window in it exists   
    if(infoWindow)
    infoWindow.close();
    // Create new infoWindow
         infoWindow = new google.maps.InfoWindow({
            content: info
        });
    
    // Create marker
        var marker = new google.maps.Marker({
            position: new google.maps.LatLng(lat, lng),
            title: 'Lima',
            map: map.map,
            infoWindow: infoWindow
        });
    
    // This opens the infoWindow
        infoWindow.open(map, marker);
    }
    

    如果有多个信息窗口,您可以有一个数组,遍历它们并在每个项目上调用close() 方法..

    【讨论】:

      【解决方案2】:

      试试这个代码..

      var infowindows = new Array();
      var popup = new google.maps.InfoWindow({
        // size: new google.maps.Size(420,130),
          content:content_info
      });
      infowindows.push(popup); //push all infoboxes to an array
      
      google.maps.event.addListener(marker, 'click', function() {
          close_popups();    //close any infobox open
          map.panTo(mycenter1);
          popup.open(map, marker); //opens current marker's infowindow
      
      
          // currentPopup = null;
        });
      
      function close_popups(){
        for(var i = 0; i<infowindows.length; i++){
          infowindows[i].close();
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-31
        • 1970-01-01
        • 2018-12-06
        • 1970-01-01
        • 2019-06-01
        相关资源
        最近更新 更多