2012-03-29 75 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