0

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

我目前正在做的是编码:

var encoded_path = google.maps.geometry.encoding.encodePath(coordinate_array)

绘制线的 lat lng 坐标数组(在折线应用程序内),并将其传递给方向服务路线,如下所示(在另一个应用程序内):

var coordinates = google.maps.geometry.encoding.decodePath(encoded_path);

var request = {
   origin: coordinates[0],
   destination: coordinates[coordinates.length - 1],
   travelMode: google.maps.DirectionsTravelMode.DRIVING
};

MapService.directionsService.route(request, function(response, status) {
   if (status == google.maps.DirectionsStatus.OK) {
      MapService.directionsDisplay.setDirections(response);
   }
});

这种方法的问题是它只使用折线的起点和终点来绘制路线,因此沿路线的所有改道都没有显示出来。所以我尝试添加航点(谷歌限制为 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;
   }
}

这样它就可以在坐标数组中均匀地获取航点。然后我试图在我的路由调用中像这样显示它们:

var request = {
   origin: coordinates[0],
   destination: coordinates[coordinates.length - 1],
   waypoints: waypoints
   travelMode: google.maps.DirectionsTravelMode.DRIVING
};

但我收到此错误:错误:在属性航点中:在索引 0:未知属性 lb

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

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

谢谢你。

4

1 回答 1

1

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

DirectionsRequest 对象定义的“航点”属性应该是 google.maps.DirectionsWaypoint 对象定义的数组https://developers.google.com/maps/documentation/javascript/3.exp/reference#DirectionsWaypoint

所以,试试:

waypoints.push(
    {
        location: new google.maps.LatLng(coordinates[index].lat(), coordinates[index].lng())
    }
);
于 2013-10-21T05:20:28.050 回答