I'm trying to put out some Google Maps-markers on my map with location from a API. I tried to make a for-loop that counts all JSON-data and puts "A drivers name, a drivers latitude and a drivers longitude" inside a array. Then should this array help Google Maps to pin-out theese locations on a map. Code example below:
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);
This code is inside a interval due to I want it to reload theese positions during all the time. I looked into errors I got in Google Chrome web developer tool, but then I just got more errors. Is there an easy solution to this? A real "nut-cracker".
Thanks in advance!
Jack