我的网站上有谷歌地图,其中显示了英国各地的位置列表。但是信息窗口为每个单独的记录显示相同的记录。我的代码在下面。有什么理由吗?
var locations = [[52.478899, -1.894055, 'test 1', '87', 'Name: test 1'],
[52.479474, -1.895946, 'test 2', '88', 'Name: test 2 '],
[52.477082, -1.893929, 'test 3', '89', 'Name: test 3 ']];
function initialize() {
var mapOptions = {
zoom: 8,
center: new google.maps.LatLng(52.528680, -1.839480),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
var bounds = new google.maps.LatLngBounds();
for (var i = 0; i < locations.length; i++) {
var curLoc = locations[i];
myLatLng = new google.maps.LatLng(curLoc[0], curLoc[1]);
var beachMarker = new google.maps.Marker({
position: myLatLng,
map: map,
title: curLoc[2],
data: curLoc[4], //add content to the Marker-object
zIndex: 50 - i
});
var infowindow = new google.maps.InfoWindow({
content: curLoc[4]
});
google.maps.event.addListener(beachMarker, 'click', (function() {
infowindow.setContent(beachMarker.data);
infowindow.setPosition(beachMarker.position);
infowindow.open(map);
}));
bounds.extend(myLatLng);
map.fitBounds(bounds);
}
}
google.maps.event.addDomListener(window, 'load', initialize);
更新:我更改了我的代码,但问题仍然存在,但现在单击标记会关注最后一个标记,而不仅仅是显示最后一个标记文本。