Google maps 谷歌地图路线生成与航路点

Google maps 谷歌地图路线生成与航路点,google-maps,google-maps-api-3,Google Maps,Google Maps Api 3,我有一个现有的应用程序可以跟踪车辆并在地图上渲染它们的多段线,我希望能够使用路由服务将这些多段线导入到另一个应用程序中(以便导入的多段线捕捉到道路并可以四处拖动等) 我目前正在做的是编码: var encoded_path = google.maps.geometry.encoding.encodePath(coordinate_array) 绘制直线(在多段线应用程序内)并将其传递到方向服务路线(如其他应用程序内)的lat lng坐标阵列: 这种方法的问题在于,它只使用多段线的起点和终点来绘

我有一个现有的应用程序可以跟踪车辆并在地图上渲染它们的多段线,我希望能够使用路由服务将这些多段线导入到另一个应用程序中(以便导入的多段线捕捉到道路并可以四处拖动等)

我目前正在做的是编码:

var encoded_path = google.maps.geometry.encoding.encodePath(coordinate_array)
绘制直线(在多段线应用程序内)并将其传递到方向服务路线(如其他应用程序内)的lat lng坐标阵列:

这种方法的问题在于,它只使用多段线的起点和终点来绘制路线,因此不会显示沿路线的所有改道。因此,我尝试添加航路点(谷歌限制为8个),以获得稍微更精确的路线,如下所示:

var waypoints = [];

if (coordinates.length <= 8) {
   waypoints = coordinates;
}
else {
   for (var i = 0; i < 8; i++) {
      var index = Math.floor((coordinates.length/8) * i);

      // Break if there's no more waypoints to be added
      if (index > coordinates.length - 1)
         break;

      waypoints.push(new google.maps.LatLng(coordinates[index].lat(), coordinates[index].lng()));

      // Break if we've just added the last waypoint
      if (index == coordinates.length - 1)
         break;
   }
}
但我得到了这个错误:错误:在属性航路点:在索引0处:未知属性lb

有人知道会发生什么,或者如何做这个航路点的事情吗?我可以通过控制台确认数组是否正确生成,下面是第一个数组元素的示例:

Array[8]
  0: N
    lb: -22.39019
    mb: 143.04560000000004
    __prot__: N
  1:...etc etc
多谢各位

waypoints.push(新的google.maps.LatLng(坐标[index].lat(),坐标[index].lng())

DirectionRequest对象定义的“waypoints”属性应该是google.maps.DirectionsWaypoint对象定义的数组

因此,请尝试:

waypoints.push(
    {
        location: new google.maps.LatLng(coordinates[index].lat(), coordinates[index].lng())
    }
);

谢谢,你说得对。我还需要中途停留:false,但你不知道,因为这是我的应用程序的一个特定部分。更进一步,事实证明这实际上不起作用。如果我使用通过使用route service绘制路线而生成的编码路径,但将多段线中的坐标作为航路点传递会导致整个路线不渲染(可能是因为某些坐标可能稍微偏离道路),则该方法是有效的。我希望谷歌能将其捕捉到最近的有效坐标,但我想不会。
Array[8]
  0: N
    lb: -22.39019
    mb: 143.04560000000004
    __prot__: N
  1:...etc etc
waypoints.push(
    {
        location: new google.maps.LatLng(coordinates[index].lat(), coordinates[index].lng())
    }
);