a我知道这是一个重复的问题,
我在基于 Django 的网络应用程序上使用谷歌地图 v3。我使用标记、信息窗口和折线
。一切工作正常,除了当我单击标记以通过信息窗口显示内容时,之前打开的信息窗口没有关闭。
我发布了我的 map 代码(仅脚本部分或哪些是有用的):
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;
}
关于javascript - 如何在谷歌地图V3中同时打开单个信息窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11428017/