首页 文章

使用航点的Google Map 路线生成

提问于
浏览
0

我有一个现有的应用程序跟踪车辆并在 Map 上呈现他们的折线,我希望能够使用路由服务将这些折线导入另一个应用程序(以便导入的折线捕捉到道路并可以拖动等等) .

我目前正在做的是编码:

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

谢谢 .

1 回答

  • 1

    waypoints.push(new google.maps.LatLng(coordinates [index] .lat(),coordinates [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())
        }
    );
    

相关问题