如何动态地在谷歌地图上绘制折线

时间:2013-10-25 15:36:05

标签: javascript google-maps

我无法在谷歌地图上绘制折线。我正在动态获取价值。

var flightPlanCoordinates=[];
n = "new google.maps.LatLng(";
q = ")";
var flightPlanCoordinates = new Array();
for(i=0;i<len;i++)
{  

  o =n+r[i].split(',')[0]+","+r[i].split(',')[1]+q;

  flightPlanCoordinates.push(o);   

}   


var flightPath = new google.maps.Polyline({
 path: flightPlanCoordinates,
 geodesic: true,
 strokeColor: '#FF0000',
 strokeOpacity: 1.0,
 strokeWeight: 2
 });

flightPath.setMap(map);
google.maps.event.addDomListener(window, 'load', initialize);

我有一个像这样的列表r = ['13.034056,80.250489 | 13.036324,80.248538 | 13.026394,80.237562']。 我有一个问题上面的部分。但是我能够静态传递值,并且我按照预期得到了折线。提前谢谢。

1 个答案:

答案 0 :(得分:6)

这适合我。

var r=['13.034056,80.250489|13.036324,80.248538|13.026394,80.237562'];
var coordinates = r[0].split("|");
var flightPlanCoordinates = new Array();
for(i=0;i<coordinates.length;i++)
{  
  var point =new google.maps.LatLng(coordinates[i].split(',')[0],coordinates[i].split(',')[1]);
  flightPlanCoordinates.push(point);   
}   


var flightPath = new google.maps.Polyline({
 path: flightPlanCoordinates,
 geodesic: true,
 strokeColor: '#FF0000',
 strokeOpacity: 1.0,
 strokeWeight: 2
 });

flightPath.setMap(map);
}
google.maps.event.addDomListener(window, 'load', initialize);

working example