如何在Leaflet地图上更新和生成标记?

时间:2013-05-13 13:09:32

标签: jquery ajax json api map

我刚刚发现Leaflet并将使用它而不是谷歌地图。我有一个API,我希望生成和更新地图标记(或者,我将有多个标记)。我在这里有一个小提琴http://goo.gl/rI5YH,我已经做了一段时间了。问题是我似乎无法修复从API中获取JSON的函数,然后更新地图并在其上放置标记。我有一堆代码所以请看我附带的小提琴:http://goo.gl/rI5YH

1 个答案:

答案 0 :(得分:0)

Here是答案,由于跨域限制我无法进行Ajax调用,所以我在代码中实例化了你的json,但这在任何方面都是微不足道的。

这是修改后的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