2012-03-29 74 views
0

我正在收集歷史負載併爲每個位置添加一個地圖標記,只是想知道我怎麼也可以在每個標記之間繪製折線?爲每個位置創建gmap3折線

jQuery.each(json, function(i, item) { 
       var name = item.name; 
       var userId = item.data.user; 
       jQuery.each(item.data.data, function(i, nav) { 
        var ts = nav.timestamp; 
        var lat = nav.latitude; 
        var long = nav.longitude; 

        if (lat != null && long != null) { 
         addMarker(name, counts = counts + 1, ts, lat, long, userId); 
        } 
       }); 
      }) 

我創建了一個簡單的函數來繪製折線

function addPolyline() { 
    jQuery("#gMap").gmap3({ 
     action: 'addPolyline', 
     options:{ 
     strokeColor: "#FF0000", 
     strokeOpacity: 1.0, 
     strokeWeight: 2 
     }, 
     path:[ 
     [37.772323, -122.214897], 
     [21.291982, -157.821856], 
     [-18.142599, 178.431], 
     [-27.46758, 153.027892] 
     ] 
    }); 

}

而是奮力fugure如何我可以借鑑在foreach數據折線?

回答

1

在您的JSON數據假設你已經點的陣列,或者可以構建一個從數據的部分修改addPolyline()函數接受數組作爲參數

var polyArray=[ 
      [37.772323, -122.214897], 
      [21.291982, -157.821856], 
      [-18.142599, 178.431], 
      [-27.46758, 153.027892] 
      ]; 

    function addPolyline(polyArray) { 
     jQuery("#gMap").gmap3({ 
      action: 'addPolyline', 
      options:{ 
      strokeColor: "#FF0000", 
      strokeOpacity: 1.0, 
      strokeWeight: 2 
      }, 
      path:polyArray 
     }); 

    } 

不知道,如果你想打電話這在你的數據的每個循環上或者從每個循環存儲數據到一個數組中,並且在最後只調用一次。

如果循環後的一個時間只能稱之爲:

var polyArray=[]; 

    $.each(json, function(i,item){ 
     /* your other parsing code....plus add to polyArray*/     
     polyArray.push([ item.someValue, item.otherValue]);    

    }); 
    addPolyline(polyArray); 
+0

的偉大工程謝謝 – 2012-03-30 14:57:08