Question

I've just discovered Leaflet and will be using that instead of Google Maps. I have an API where I want map markers to be generated from and updated (or else, I'll have multiple markers). I have a fiddle over here http://goo.gl/rI5YH which I've been working on for a while. Problem is that I can't seem to fix a function that grabs JSON from an API, and then updates the map and puts markers on it. I have a bunch of code so please see my attached fiddle: http://goo.gl/rI5YH .

Was it helpful?

Solution

Here is the answer, due to cross domain restrictions I couldn't make Ajax Calls,so I instantiated your json in the code, but thats pretty trivial any ways.

Here is the modified JS

init(); //Calls the "grab my locayion" funcyion
function init() {
    var map = L.map('map', {
        center: [51.505, -0.09],
        zoom: 13
    })

    L.tileLayer('http://{s}.tile.cloudmade.com/1fa9625d371549a298938509a2eac256/997/256/{z}/{x}/{y}.png').addTo(map);



var drivers = 
     [{
            "name": "Jack Billström",
            "profileId": "3",
            "facebook_id": "100000650223192",
            "email": "jack@codele.se",
            "phone": "727396760",
            "kikId": "jackiboi95",
            "currentLat": "62.457201",
            "currentLon": "17.350931",
            "isDriver": "1"
        }, {
            "name": "John Doe",
            "profileId": "2",
            "facebook_id": "0",
            "email": "johndoe@codele.se",
            "phone": "700000000",
            "kikId": "johndoe",
            "currentLat": "62.442671",
            "currentLon": "17.338829",
            "isDriver": "1"
        }, {
            "name": "Andreas Ekström",
            "profileId": "1",
            "facebook_id": "didair",
            "email": "didair@msn.com",
            "phone": "739620011",
            "kikId": "didair",
            "currentLat": "62.457201",
            "currentLon": "17.350931",
            "isDriver": "1"
        }
    ];
for (var i=0;i<drivers.length;i++)
{
    var driver=drivers[i];
  var m = L.marker(new L.LatLng(driver.currentLat,driver.currentLon)).addTo(map)
            .bindPopup("<center><b>"+driver.name +"</b></center>").openPopup();
}

}
// API-URL: http://blackcab.didair.se/api/drivers
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top