-1
我正在嘗試使用jQuery和javascript生成谷歌地圖。緯度和經度值存儲在JSON文件中。當用戶點擊需要生成的狀態名稱時。通過JSON循環以在Google地圖上放置標記
我的代碼存在的問題是,圖形只能生成index[0]
。我的for循環無法正常工作。產生紐約的圖表,但它指向尼泊爾,而不是紐約。這裏是我的代碼:
for (var i in data.universitylist) {
var lat = data.universitylist[i].lat;
var lng = data.universitylist[i].lng;
var map = new google.maps.Map(document.getElementById('map'), {
center: {
lat: lat,
lng: lng
},
scrollwheel: false,
zoom: 8
});
var marker = new google.maps.Marker({
map: map,
position: {
lat: lat,
lng: lng
},
title: 'Hello World!'
});
};
的JSON文件:
您的問題是要重新生成,var data = {
"universitylist": [{
"Name": "Columbia University",
"City": "New York",
"State": "NY",
"lat": 40.7127,
"lng": 74.0059
},{
"Name": "Stanford University",
"City": "Stanford,",
"State": "CA",
"lat": 37.0000,
"lng": 120.0000
},{
"Name": "Duke University",
"City": "Durham",
"State": "NC",
"lat": 35.5000,
"lng": 80.0000
},{
"Name": "University of Pennsylvania",
"City": "Philadelphia",
"State": "PA",
"lat": 41.0000,
"lng": 77.5000
},{
"Name": "Northcentral University",
"City": "Prescott Valley",
"State": "AZ",
"lat": 34.0000,
"lng": 112.0000
},{
"Name": "Johns Hopkins University",
"City": "Baltimore",
"State": "MD",
"lat": 39.0000,
"lng": 76.7000
}, {
"Name": "Northwestern University",
"City": "Evanston",
"State": "IL",
"lat": 40.0000,
"lng": 89.0000
},{
"Name": "Rice University",
"City": "Houston",
"State": "TX",
"lat": 31.0000,
"lng": 100.0000
},{
"Name": "University of Southern California",
"City": "Los Angeles",
"State": "CA",
"lat": 37.0000,
"lng": 120.0000
},{
"Name": "Georgia Institute of Technology",
"City": "Atlanta",
"State": "GA",
"lat": 32.9605,
"lng": 83.1132
}
]
};
現在你已經工作正常的代碼:https://jsfiddle.net/f4ksqyz5/。你能否在http://jsfiddle.net上顯示這個問題的例子。如果標記放置在錯誤的地方,那麼聽起來就像你的緯度/經度值不正確。 –