我正嘗試在地圖上放置一些Google地圖標記,並使用API的位置。我試圖做一個for-loop計算所有JSON數據,並在數組中放入「A drivers name,a drivers latitude and a driver longitude」。那麼這個數組是否應該幫助谷歌地圖在地圖上標出這些位置。下面的代碼示例:如何從外部JSON數據源在Google地圖上放置標記?
setTimeout(function() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: new google.maps.LatLng(62.457171, 17.350953),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
var marker, i;
var url = "http://blackcab.didair.se/api/drivers";
var name;
var lat;
var lon;
var locations;
$.getJSON(url,
function (response) {
name = response.drivers[n].name;
lat = response.drivers[n].currentLat;
lon = response.drivers[n].currentLon;
for (var n = 0; n < response.drivers.length; n++)
var locations = [
[name, lat, lon, n], ];
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
});
google.maps.event.addListener(marker, 'click', (function (marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
});
}, 3000);
此代碼是一個區間內,由於我希望它在所有的時間重裝theese位置。 我查看了Google Chrome網絡開發者工具中的錯誤,但後來發現了更多錯誤。有沒有簡單的解決方案?一個真正的「堅果餅乾」。
在此先感謝!
傑克
試行代碼現在... – 2013-04-11 15:46:15
你從別的地方複製這個代碼? – 2013-04-11 16:01:33
我從這個源複製它的一些:http://stackoverflow.com/questions/3059044/google-maps-js-api-v3-simple-multiple-marker-example然後試圖把我的代碼從驅動程序我的API。 – Jack 2013-04-11 16:02:55