【问题标题】:How to keep single Info window open at the same time in Google map V3?如何在谷歌地图 V3 中保持单个信息窗口同时打开?
【发布时间】:2023-04-08 14:13:01
【问题描述】:

我知道这是重复的问题,

我在基于 Django 网络的应用程序上使用 google map v3。我在哪里使用Markers, Infowindow and polyline。一切正常,除了当我单击标记以通过信息窗口显示内容时,之前打开的信息窗口没有关闭。

我正在发布我的地图代码(仅脚本部分或有用的部分):

var marker = add_marker(flightPlanCoordinates[i][0], flightPlanCoordinates[i][1],"Event Detail",myHtml);

这里myHtml 是一个包含信息窗口内容的变量。我没有在这里定义变量。所以忽略它。

    marker.setMap(map);
    }

    var flightPath = new google.maps.Polyline({
                 path: flightPlanCoordinatesSet,
                 strokeColor: "#FF0000",
                 strokeOpacity: 1.0,
                 strokeWeight: 2
                  });
    flightPath.setMap(map);
}

function add_marker(lat,lng,title,box_html) {
var infowindow = new google.maps.InfoWindow({
    content: box_html
});

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

google.maps.event.addListener(marker, 'click', function() {
  infowindow.open(map,this);
});   

return marker;
}

【问题讨论】:

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


    【解决方案1】:

    而不是多个 infoWindows 只使用一个实例。

    点击标记时,先关闭infoWindow,然后设置新内容,再打开infoWindow。

    function add_marker(lat,lng,title,box_html) 
    {
      //create the global instance of infoWindow 
      if(!window.infowindow)
      {
        window.infowindow=new google.maps.InfoWindow();
      } 
    
      var marker = new google.maps.Marker({
          position: new google.maps.LatLng(lat,lng),
          map: map,
          title: title
      });
    
      google.maps.event.addListener(marker, 'click', function() {
        infowindow.close();
        infowindow.setContent(box_html);
        infowindow.open(map,this)
      });   
    
      return marker;
    }
    

    【讨论】:

    • 感谢您的回复,我尝试使用此选项。但没用。可能是我错了你能不能给我看一下添加监听器方法的伪代码
    • @Dr.Molle 我在LatLngBounds 中有多个标记,如果我创建 infoWindow 的全局实例,最后一个 infowindow 会显示所有指针
    • @Blue Bells:你能发一个页面链接吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-18
    • 2011-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-16
    • 2013-12-16
    相关资源
    最近更新 更多