如何在谷歌地图V3中保持单个信息窗口同时打开

How to keep single Info window open at the same time in Google map V3?

本文关键字:窗口 信息窗 信息 单个 谷歌地图 V3      更新时间:2023-09-26

aI知道这是一个重复的问题,

我在Django基于web的应用程序上使用谷歌地图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;
}

不使用多个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;
}