单击其他位置时Google Maps API关闭信息窗口 [英] Google maps api close infowindow when clicking somewhere else
问题描述
我已经尝试过使用google,但找不到适用于我的代码的那个.我希望我的信息窗口在有人按下地图上的另一个位置或在其他位置打开另一个信息窗口时关闭.
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小提琴中检出代码已添加
google.maps.event.addListener(map, 'click', function(){
infowindow1.close(map, marker1);
});
很显然,要捕获地图上所有未完成的项目,您必须使用一个小循环来捕获该函数中的所有内容,但这就是我要留给您的东西.我将添加一个变量来跟踪您创建了多少个变量,然后将"infowindow" + var_index循环到该最大值,将'em all'设置为close.我仍在寻找一种更好的方法来获取所有打开的信息窗口,但与此同时,该技术也可以使用.
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 Maps API关闭信息窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!