Javascript 在谷歌地图中显示来自已定义路线的多条路线

Javascript 在谷歌地图中显示来自已定义路线的多条路线,javascript,google-maps,routes,Javascript,Google Maps,Routes,我需要在GoogleMaps中映射变量testf中已经定义的路由。我需要追踪一条从点Id 1到点Id 2的路线,然后画另一条从点Id 3到点Id 4的路线,即每对点的不同路线,怎么办?我无法让谷歌地图理解如何通过传递包含所有这些点的变量来从起点读取到终点。这只是javascript中我无法实现的部分: var testef = [{ "Id": 1, "Latitude": 38.726177, "Longitude": -9.180716 }, { "Id": 2, "Latitude": 3

我需要在GoogleMaps中映射变量testf中已经定义的路由。我需要追踪一条从点Id 1到点Id 2的路线,然后画另一条从点Id 3到点Id 4的路线,即每对点的不同路线,怎么办?我无法让谷歌地图理解如何通过传递包含所有这些点的变量来从起点读取到终点。这只是javascript中我无法实现的部分:

var testef = [{
"Id": 1,
"Latitude": 38.726177,
"Longitude": -9.180716
},
{
"Id": 2,
"Latitude": 38.716177,
"Longitude": -9.170716
},
{
"Id": 3,
"Latitude": 38.736177,
"Longitude": -9.160716
},
 {
"Id": 4,
"Latitude": 38.729177,
"Longitude": -9.110716
 }];

 //traçando a rota
for(var k=0; k < testef.length; k++){
    var objk=testef[k];

    var mypath = new google.maps.LatLng(objk.Latitude,objk.Longitude);

    var teste = new google.maps.Polyline({
        path: mypath,
        geodesic: true,
        strokeColor: '#ff0000',
        strokeOpacity: 1.0,
        strokeWeight: 3
    });
} 

  teste.setMap(map); 
var testef=[{
“Id”:1,
“纬度”:38.726177,
“经度”:-9.180716
},
{
“Id”:2,
“纬度”:38.716177,
“经度”:-9.170716
},
{
“Id”:3,
“纬度”:38.736177,
“经度”:-9.160716
},
{
“Id”:4,
“纬度”:38.729177,
“经度”:-9.110716
}];
//轮值茶安多
对于(var k=0;k

我希望画出几个已经定义的路由

我得到一个javascript错误,发布的代码是:
InvalidValueError:不是数组
,因为mypath的值不是数组

  • 通过阵列成对创建点,使用每组点(每个到点线段的路径)创建阵列:

for (var k = 0; k < testef.length-1; k+=2) {
    // start of segment
    var objk = testef[k];
    var pt = new google.maps.LatLng(objk.Latitude, objk.Longitude);
    // end of segment 
    var objkp1 = testef[k+1];
    var pt1 = new google.maps.LatLng(objkp1.Latitude, objkp1.Longitude);
    // create the path for this segment
    var mypath = [pt, pt1];
    // create the polyline for this segment
    var teste = new google.maps.Polyline({
      path: mypath,
      geodesic: true,
      strokeColor: '#ff0000',
      strokeOpacity: 1.0,
      strokeWeight: 3
    });
    // add this segment to the map
    teste.setMap(map);
}