如何关闭信息窗口
我已经有一系列标记,我想在单击另一个标记时关闭打开的信息窗口。 我知道使用 v3 API,我可以使用 InfoWindow.close() 方法关闭信息窗口。但是在哪里添加代码?我尝试了很多但失败了。 我还有一个问题,为什么我需要点击“side_bar_div”中的链接来调用方法myclick(i)?
<script type="text/javascript">
var markers=[];
var side_bar_html='<table border=0 cellpadding=0><tr>';
var tdrow = 1;
var i = 0;
function creatmarker(lat,long,map,html,index){
var mylatlng = new google.maps.LatLng(lat,long);
var marker = new google.maps.Marker({
position: mylatlng,
map: map,
zIndex: index
});
var infowindow = new google.maps.InfoWindow({
content: html
});
markers.push(marker);
side_bar_html +='<td width=330 valign=top align=left><a id="side_bar_'+i+'" href="javascript:myclick('+ (markers.length-1) +');">'+html+'</a></td>';
tdrow++;
i++;
if (tdrow > 2){side_bar_html += '</tr><tr>'; tdrow =1; }
google.maps.event.addListener(marker,'click',function(){
infowindow.open(map,marker);
});
function myclick(i){
var id="side_bar_"+i;
google.maps.event.addDomListener(document.getElementById(id), "click", function(){ google.maps.event.trigger(markers[i], "click");
});
}
function initialize(){
var myOptions = {
zoom: 11,
center: new google.maps.LatLng(-33.9, 151.2),
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("mymap"),myOptions);
var lat = "-33.890542";
var lng = "151.274856";
var html = 'Bondi Beach';
creatmarker(lat,lng,map,html,5);
var lat = "-33.923036";
var lng = "151.259052";
var html = 'Coogee Beach';
creatmarker(lat,lng,map,html,4);
var lat = "-34.028249";
var lng = "151.157507";
var html = 'Cronulla Beach';
creatmarker(lat,lng,map,html,3);
var lat = "-33.80010128657071";
var lng = "151.28747820854187";
var html = 'Manly Beach';
creatmarker(lat,lng,map,html,2);
var lat = "-33.950198";
var lng = "151.259302";
var html = 'Maroubra Beach';
creatmarker(lat,lng,map,html,1);
side_bar_html +='</tr></table>';
document.getElementById("side_bar_div").innerHTML = side_bar_html;
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
I have already have an array of markers, I want to close an opened infowindow when I click another marker.
I know that with the v3 API, I can close a infowindow with the InfoWindow.close() method.But where to add the code?I have tried a lot but failed.
I have another question ,why need I click the link in "side_bar_div" tiwce to call the method myclick(i)?
<script type="text/javascript">
var markers=[];
var side_bar_html='<table border=0 cellpadding=0><tr>';
var tdrow = 1;
var i = 0;
function creatmarker(lat,long,map,html,index){
var mylatlng = new google.maps.LatLng(lat,long);
var marker = new google.maps.Marker({
position: mylatlng,
map: map,
zIndex: index
});
var infowindow = new google.maps.InfoWindow({
content: html
});
markers.push(marker);
side_bar_html +='<td width=330 valign=top align=left><a id="side_bar_'+i+'" href="javascript:myclick('+ (markers.length-1) +');">'+html+'</a></td>';
tdrow++;
i++;
if (tdrow > 2){side_bar_html += '</tr><tr>'; tdrow =1; }
google.maps.event.addListener(marker,'click',function(){
infowindow.open(map,marker);
});
function myclick(i){
var id="side_bar_"+i;
google.maps.event.addDomListener(document.getElementById(id), "click", function(){ google.maps.event.trigger(markers[i], "click");
});
}
function initialize(){
var myOptions = {
zoom: 11,
center: new google.maps.LatLng(-33.9, 151.2),
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("mymap"),myOptions);
var lat = "-33.890542";
var lng = "151.274856";
var html = 'Bondi Beach';
creatmarker(lat,lng,map,html,5);
var lat = "-33.923036";
var lng = "151.259052";
var html = 'Coogee Beach';
creatmarker(lat,lng,map,html,4);
var lat = "-34.028249";
var lng = "151.157507";
var html = 'Cronulla Beach';
creatmarker(lat,lng,map,html,3);
var lat = "-33.80010128657071";
var lng = "151.28747820854187";
var html = 'Manly Beach';
creatmarker(lat,lng,map,html,2);
var lat = "-33.950198";
var lng = "151.259302";
var html = 'Maroubra Beach';
creatmarker(lat,lng,map,html,1);
side_bar_html +='</tr></table>';
document.getElementById("side_bar_div").innerHTML = side_bar_html;
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试以下代码片段:
希望这对您有帮助:-)
Try the following code snippet:
Hope this helps you :-)
正确的方法是将其存储在局部变量中,然后添加一个事件侦听器,如下所示:
currentinfowindow 是一个全局变量,仅保存打开的信息窗口(如果存在)。
无需使用数组或额外的方法:)
The correct way to do this is to store it in a local variable and then add an eventlistener like this:
currentinfowindow is a global variable that will only hold the open infowindow if one exists.
No need to use an array or an extra method :)