2013-05-13 61 views
1

我剛剛發現了Leaflet,並將使用它來代替Google Maps。我有一個API,我希望從中生成地圖標記並進行更新(否則,我將擁有多個標記)。我在這裏有一個小提琴http://goo.gl/rI5YH,我一直在努力。問題是我似乎無法修復從API獲取JSON的函數,然後更新地圖並在其上放置標記。我有一堆代碼,所以請看我附帶的小提琴:http://goo.gl/rI5YH如何在傳單地圖上更新和生成標記?

回答

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": "[email protected]", 
      "phone": "727396760", 
      "kikId": "jackiboi95", 
      "currentLat": "62.457201", 
      "currentLon": "17.350931", 
      "isDriver": "1" 
     }, { 
      "name": "John Doe", 
      "profileId": "2", 
      "facebook_id": "0", 
      "email": "[email protected]", 
      "phone": "700000000", 
      "kikId": "johndoe", 
      "currentLat": "62.442671", 
      "currentLon": "17.338829", 
      "isDriver": "1" 
     }, { 
      "name": "Andreas Ekström", 
      "profileId": "1", 
      "facebook_id": "didair", 
      "email": "[email protected]", 
      "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 
+0

謝謝@varun!救星! – Jack 2013-05-13 20:02:10

相關問題