【问题标题】:Google Maps Route Generation with Waypoints谷歌地图路线生成与航点
【发布时间】:2013-10-21 04:37:55
【问题描述】:

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

我目前正在做的是编码:

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

谢谢。

【问题讨论】:

    标签: google-maps google-maps-api-3


    【解决方案1】:

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

    DirectionsRequest 对象定义的 'waypoints' 属性应该是一个 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())
        }
    );
    

    【讨论】:

    • 谢谢你,你是对的。我还需要中途停留:false,但您不知道这一点,因为它是我的应用程序的特定部分。
    • 更进一步,事实证明这实际上是行不通的。如果我使用通过使用路线服务绘制路线生成的编码路径,它会起作用,但是从折线传递坐标作为航点会导致整个路线不渲染(可能是因为某些坐标可能稍微偏离道路) .我希望谷歌能将它捕捉到最近的有效坐标,但我想不会。
    猜你喜欢
    • 1970-01-01
    • 2019-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-28
    相关资源
    最近更新 更多