[英]google maps api v3: infowindows distorted
当我插入x的值而不是使用for循环时,此代码有效。 但是,当我使用for循环时,信息窗口在屏幕左侧显示为变形。
到底是怎么回事!
for(var x = 0; x <data.page_size; x ++){
var position = new google.maps.LatLng( data.events.event[x]['latitude'], data.events.event[x]['longitude']); marker.push( new google.maps.Marker({ position: position, map: map, icon: image} )); google.maps.event.addListener(marker[x], 'click', function() { infowindow.setContent(content[x]); infowindow.open(map, marker[x]); });
}
在这种情况下,您必须使用闭包。 像这样:
(function(m,c){
google.maps.event.addListener(m, 'click', function() {
infowindow.setContent(c);
infowindow.open(map, m);
});
})(marker[x],content[x])
解决此问题的一种经济方法是使标记意识到自己的索引,而每个标记仅具有一个Number属性,而无需形成一组包含content[x]
的闭包。
for (var x = 0; x < data.page_size; x++) {
...
marker[x].x = x;//make the marker aware of its own index
google.maps.event.addListener(marker[x], 'click', function() {
infowindow.setContent(content[this.x]);
infowindow.open(map, this);
});
}
如果marker
数组和content
数组保持静态或受到兼容管理,则this.x
将可靠地为每个标记提取正确的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.