我通过for循环从xml向Google Map添加了许多标记。当我单击要弹出的信息窗口的标记时,出现一个错误,指出“无法调用未定义的方法“打开””。我在这里做错了什么?
jQuery的
var markers = xml.documentElement.getElementsByTagName('marker');
//function to create unique id
var getMarkerUniqueId = function(lat, lng) {
return lat + '_' + lng;
}
//function to get lat/lng
var getLatLng = function(lat,lng) {
return new google.maps.LatLng(lat, lng);
}
//cycle through and create map markers for each xml marker
for (var i = 0; i < markers.length; i++) {
//create vars to hold lat/lng from database
var lat = parseFloat(markers[i].getAttribute('lat'));
var lng = parseFloat(markers[i].getAttribute('lng'));
//create a unique id for the marker
var markerId = getMarkerUniqueId(lat, lng);
var name = markers[i].getAttribute('Type');
var html = '<b>' + name + '</b>';
//create the marker on the map
var marker = new google.maps.Marker({
map: the_Map,
position: getLatLng(lat, lng),
id: 'marker_' + markerId
});
//put the markerId into the cache
markers_arr[markerId] = marker;
infoWindow[i] = new google.maps.InfoWindow({
content: html,
position: getLatLng(lat, lng),
});
infobox[i] = google.maps.event.addListener(marker,'click',function() {
infoWindow[i].open(the_Map,marker);
});
}
最佳答案
在您执行infoWindow [i] .open时,i的值等于markers.length。您应该为每个信息窗口创建一个上下文
修改代码:
function createContext (marker, iw){
google.maps.event.addListener(marker,'click',function() {
iw.open(the_Map,marker);
/ });
}
for (var i = 0; i < markers.length; i++) {
....
infobox[i] = createContext(marker, infoWindow[i]);
}