[英]Google Maps API V3 infoWindow - All infoWindows displaying same content
在我的页面中,纬度,经度和信息窗口的内容都存在于名为obj的对象数组中。
因此,如果我需要第一个对象的纬度,可以调用obj [0] .latitude。
每个对象的infoWindow内容存储在obj [i] .text中。
我正在使用下面的循环遍历纬度和经度值,并显示标记和infoWindows。
for (x=1; x<=obj.length; x++) { var latlng1 = new google.maps.LatLng(obj[x].latitude, obj[x].longitude);
var marker2 = new google.maps.Marker({ position : latlng1, map : map, icon : prevIcon }); infowindow = new google.maps.InfoWindow(); info = obj[x].text; google.maps.event.addListener(marker2, 'mouseover', function() { infowindow.setContent(info); infowindow.open(map, this); });
}
for (x=1; x<=obj.length; x++) { var latlng1 = new google.maps.LatLng(obj[x].latitude, obj[x].longitude);
var marker2 = new google.maps.Marker({ position : latlng1, map : map, icon : prevIcon }); infowindow = new google.maps.InfoWindow(); info = obj[x].text; google.maps.event.addListener(marker2, 'mouseover', function() { infowindow.setContent(info); infowindow.open(map, this); });
}
for (x=1; x<=obj.length; x++) { var latlng1 = new google.maps.LatLng(obj[x].latitude, obj[x].longitude);
var marker2 = new google.maps.Marker({ position : latlng1, map : map, icon : prevIcon }); infowindow = new google.maps.InfoWindow(); info = obj[x].text; google.maps.event.addListener(marker2, 'mouseover', function() { infowindow.setContent(info); infowindow.open(map, this); });
}
上面的代码有效,但是所有的infoWindows都显示最后obj [i] .text的内容。
如何将特定infoWindow(obj [x] .text)的infoWindow内容仅与该infoWindow实例相关联?
如果有人可以指出一种解决方案而不使用jQuery或任何其他外部库,那会更好。
谢谢,
应该可以让您走得很远。
function attachMarker( data ) {
var latLng = new google.maps.LatLng( data.latitude, data.longitude );
var marker = new google.maps.Marker({
position : latLng,
map : map, // global variable?
icon : prevIcon // global variable?
});
google.maps.event.addListener(marker, 'mouseover', function() {
infowindow.setContent( data.text );
infowindow.open(map, this);
});
// add marker here.
}
// afaik, you only need 1 instance of InfoWindow if you only change content.
var infowindow = new google.maps.InfoWindow();
var i = obj.length;
while ( i-- ) {
attachMarker( obj[ i ] );
}
BGerrissen在上面的评论很有帮助。
我设法在info变量上实现了函数关闭。
以下Google网上论坛帖子说明了操作方法。
https://groups.google.com/group/google-maps-api-for-flash/browse_thread/thread/befeb9bf2d1ebcc9
感谢大家 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.