我正在尝试创建一个需要在其上带有一些标记的谷歌地图的网站我在多次读取 javascript 2D 数组时遇到问题它给了我错误
无法读取未定义的属性“0”
这是我的代码
function initialize() {
var locations = [
//'City',LatLng,LatLng,Zoom
['Egypt', 26.820553, 30.802498, 6], //Center of whole map
['Alexandria', 'Egypt'], //pointer
['Mansoura', 'Egypt'], //pointer
['Assiut', 'Egypt'] //pointer
];
var mapOptions = {
zoom: locations[0][3],
center: new google.maps.LatLng(locations[0][1], locations[0][2]),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map_canvas'),
mapOptions);
var infowindow = new google.maps.InfoWindow();
var marker, i;
var geocoder = new google.maps.Geocoder();
var image = 'flag.png';
for (i = 1; i < locations.length; i++) {
geocoder.geocode({ 'address': locations[i][0] + ' ' + locations[i][1] }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
marker = new google.maps.Marker({
animation: google.maps.Animation.DROP,
map: map,
icon: image,
position: results[0].geometry.location,
title: locations[i][0]
});
google.maps.event.addListener(marker, 'click', (function (marker, i) {
return function () {
infowindow.setContent('<b style="font-size:30px;">' + locations[i][0] + '</b>'
+ '<br><b>Starting Date:</b> 11/5/2012'
+ '<br><b>Ending Date:</b> 30/5/2012'
+ '<br><a href="google.com">Event Website</a>');
infowindow.open(map, marker);
}
})(marker, i));
}
});
}
}
function loadScript() {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://maps.googleapis.com/maps/api/js?key=AIzaSyA6yYH66F1L3NgHAobufuUF6l-jjVCLwfE&sensor=false&' +
'callback=initialize';
document.body.appendChild(script);
}
window.onload = loadScript;
当我尝试多次读取位置[i][0] 时,我在本节中遇到了错误。
for (i = 1; i < locations.length; i++) {
geocoder.geocode({ 'address': locations[i][0] + ' ' + locations[i][1] }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
marker = new google.maps.Marker({
animation: google.maps.Animation.DROP,
map: map,
icon: image,
position: results[0].geometry.location,
title: locations[i][0]
});
google.maps.event.addListener(marker, 'click', (function (marker, i) {
return function () {
infowindow.setContent('<b style="font-size:30px;">' + locations[i][0] + '</b>'
+ '<br><b>Starting Date:</b> 11/5/2012'
+ '<br><b>Ending Date:</b> 30/5/2012'
+ '<br><a href="google.com">Event Website</a>');
infowindow.open(map, marker);
}
})(marker, i));
}
});
}
如果无论如何我可以在后面的c#代码中处理这个代码会很好