我阅读了很多解决方案并尝试了几个,但在我的脚本中我无法完成它。
我喜欢在点击另一个标记时关闭活动信息窗口。
部分代码:
for (var i = 0; i < locations.length; i++) {
var centrum = locations[i];
var myLatLng = new google.maps.LatLng(centrum[1], centrum[2]);
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
animation: google.maps.Animation.DROP,
icon: image,
shape: shape,
title: centrum[0],
zIndex: centrum[3]
});
marker['infowindow'] = new google.maps.InfoWindow({
content: '<b>Informatie:</b><br>' + centrum[0]
});
google.maps.event.addListener(marker, 'click', function() {
marker['infowindow'].close(); // has to close another active window
this['infowindow'].open(map, this);
});
}
有人提示或更好......解决方案?
感谢。
答案 0 :(得分:3)
将标记保留在数组中并添加函数调用以关闭所有窗口,然后再打开当前窗口:
function closeInfos() {
for(var i=0; i<markers.length; i++)
{
markers[i]['infowindow'].close();
}
}
修改强>
使用您的代码,它看起来应该是这样的:
var markers = [];
for (var i = 0; i < locations.length; i++) {
var centrum = locations[i];
var myLatLng = new google.maps.LatLng(centrum[1], centrum[2]);
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
animation: google.maps.Animation.DROP,
icon: image,
shape: shape,
title: centrum[0],
zIndex: centrum[3]
}));
marker['infowindow'] = new google.maps.InfoWindow({
content: '<b>Informatie:</b><br>' + centrum[0]
});
markers.push(marker);
google.maps.event.addListener(marker, 'click', function() {
closeInfos();
this['infowindow'].open(map, this);
});
}
答案 1 :(得分:1)