0
所以我想用下面的代碼和json數據來使用Google Maps功能。 但我得到錯誤:位置未定義錯誤。我真的不明白爲什麼,因爲我在我的代碼中定義。 我是JavaScript新手,有任何建議使此代碼感激。json undefined var
var data = <%=JSON.stringify(Info, null, 2) & vbNewline%>;
function initialize() {
var center = new google.maps.LatLng(48.404840395764175, 2.6845264434814453);
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 3,
center: center,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var markers = [];
for (var i = 0; i < 100; i++) {
var location = data[i];
var latLng = new google.maps.LatLng(location.latitude,
location.longitude);
var marker = new google.maps.Marker({
position: latLng
});
markers.push(marker);
}
var markerCluster = new MarkerClusterer(map, markers);
}
google.maps.event.addDomListener(window, 'load', initialize);
{ "firstname": "", "lastname": "", "location": { "latitude": "48.4048403957642", "longitude": "2.68452644348145" } } { "firstname": "", "lastname": "", "location": { "latitude": "48.4050236871384", "longitude": "2.68512690051361" } } { "firstname": "", "lastname": "", "location": { "latitude": "48.4048403957642", "longitude": "2.68452644348145" } } { "firstname": "", "lastname": "", "location": { "latitude": "48.7570941168018", "longitude": "2.16670989990234" } } { "firstname": "", "lastname": "", "location": { "latitude": "48.404922961092", "longitude": "2.70020564018949" } } { "firstname": "", "lastname": "", "location": { "latitude": "48.8739279353421", "longitude": "2.32875823974609" } } { "firstname": "", "lastname": "", "location": { "latitude": "48.4048403957642", "longitude": "2.68452644348145" } } { "firstname": "", "lastname": "", "location": { "latitude": "41.0349860434783", "longitude": "28.9774059609177" } }
是你的'data'陣列中有100(或更多)的元素?如果不是,當'i'大於'data.length'時,'location'將不確定。 –
因爲我正在測試它,所以暫時沒有。 – Efe
那是你的問題。 [techfoobar的答案](http://stackoverflow.com/a/10783130/790695)告訴你如何解決它。 –