在循环中创建标记 - 在单击Listener内部时,Infowindow不会打开 [英] creating markers in loop - Infowindow does not open when inside an click Listener
问题描述
我有一个带有多个标记的谷歌地图,每个都有自己的infowindow。
当我点击时什么也没有发生。 fyi:我知道这个监听器会引发火灾,因为我之前已经提醒过它,它工作正常。
问题代码是:
google.maps.event.addListener(point [i],'click',function(){
infowindow [i] = new google.maps.InfoWindow ({content:contentString [i]});
infowindow [i] .open(map,point [i]);
});
如果我只做
$ b $
infowindow [i] = new google.maps.InfoWindow({content:contentString [i]});
infowindow [i] .open(map,point [i]);
有效。但不是在我的addListener函数中。我想这是谷歌地图不喜欢的东西,但萤火虫给我0错误。
真的需要你的帮助。非常感谢!
这是因为您可能在循环内关闭了!因此,在回调被调用时,回调中的变量 i
已被覆盖。你有两个选择如何解决它:
$ b <1>经典的循环闭包解决方法(你为每个循环迭代做另一个闭包):$ b (i = 0; i< 20; i ++){(function(i){
google.maps.event.addListener($ i))的
$ b
点[i],'点击',函数(){
infowindow [i] = new google.maps.InfoWindow({content:contentString [i]});
infowindow [i] .open地图,点[i]);
});
})(i);
}
<2>避免关闭并使用标记数据结构:
point [i] .i = i; b
$ b
google.maps.event.addListener(point [i],'click',function(){
this.myinfowindow = new google.maps.InfoWindow({content:contentString [this.i]} );
this.myinfowindow.open(map,this);
});
$ / code>
(或者您也可以将contentString也移动到标记处: point [i] .contentString = ...
并在点击处理程序中使用 this.contentString
,那么您不需要 point [i] .i
属性。)
我个人更喜欢第二种解决方案,关闭消耗内存等。
i have a google map with multiple markers and each has its own infowindow.
nothing happens when i click. fyi: i know it the listener fires because i did put a alert in ther before and it worked.
Problem Code is:
google.maps.event.addListener(point[i], 'click', function() {
infowindow[i] = new google.maps.InfoWindow({content: contentString[i] });
infowindow[i].open(map,point[i]);
});
if i only do
infowindow[i] = new google.maps.InfoWindow({content: contentString[i] });
infowindow[i].open(map,point[i]);
it works. but not in my addListener function. i guess ther is something that googlemaps doesnt like but firebug gives me 0 errors..
really need your help. Thanks a lot!
It is because you probably have closure within loop! So the variable i
in callback is already overwritten at the time when the callback is called. You have two options how to fix it:
1) classical "closure in loop" workaround (you do another closure for every loop iteration):
for (i = 0; i < 20; i++) { (function (i) {
google.maps.event.addListener(point[i], 'click', function() {
infowindow[i] = new google.maps.InfoWindow({content: contentString[i] });
infowindow[i].open(map,point[i]);
});
})(i);
}
2) avoid closure and use the marker data structure:
for (i = 0; i < 20; i++) {
point[i].i = i;
google.maps.event.addListener(point[i], 'click', function() {
this.myinfowindow = new google.maps.InfoWindow({content: contentString[this.i] });
this.myinfowindow.open(map, this);
});
}
(or you could also move the contentString also to the marker: point[i].contentString = ...
and use this.contentString
in the click handler. Then you don't need the point[i].i
attribute.)
Personally I much more prefer the 2nd solution over the first, since the closures consume memory etc.
这篇关于在循环中创建标记 - 在单击Listener内部时,Infowindow不会打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!