单击其他位置时,Google 地图 api 关闭信息窗口 [英] Google maps api close infowindow when clicking somewhere else
问题描述
我试过谷歌它,但找不到适用于我的代码的那个.我希望每次有人按下地图上的另一个位置或在其他地方打开另一个信息窗口时,我的信息窗口都关闭.
I have tried google it but can't find the one to apply to my code. i want my infowindow(s) to close everytime someone presses another place on the map or opens another infowindow somewhere else.
有人可以帮忙吗?
var myLatlng = new google.maps.LatLng(val.coords.split(',')[0].trim(),val.coords.split(',')[1].trim());
var infowindow = null;
var contentString = '<div id="content">'+
'<div id="siteNotice"> content'+
'</div>';
var infowindow1 = new google.maps.InfoWindow({
content: contentString,
maxWidth: 250
});
var marker1 = new google.maps.Marker({
position: myLatlng,
map: map,
icon: image,
});
google.maps.event.addListener(marker1, 'click', function() {
infowindow1.open(map,marker1);
});
});
});
推荐答案
在 JS Fiddle 处查看代码 I添加
Checkout the code at JS Fiddle I added
google.maps.event.addListener(map, 'click', function(){
infowindow1.close(map, marker1);
});
显然,要捕获地图上所有打开的项目,您必须使用一个小循环在该函数中捕获所有项目,但这是我留给您的事情.我会添加一个变量来跟踪您创建的数量,然后将infowindow"+var_index 循环到该最大值,将它们全部设置为关闭.我仍在寻找一种更好的方法来获取所有打开的信息窗口,但与此同时,此技术将起作用.
Obviously to catch all the open items on the map, you'd have to use a small loop to catch 'em all within that function, but that's something I'll leave to you. I would add a variable to keep track of how many you've created, then loop "infowindow"+var_index to that max value, set 'em all to close. I'm still looking for a better way to grab all open infowindows, but in the meantime this technique will work.
这篇关于单击其他位置时,Google 地图 api 关闭信息窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!