我有一个现有的应用程序,可以跟踪车辆并在地图上渲染其折线,并且我希望能够使用路由服务将这些折线导入到另一个应用程序中(以便导入的折线捕捉到道路并可以拖动) 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);
}
});
这种方法的问题在于,它仅使用折线的起点和终点来绘制路线,因此不会显示沿路线的所有改道。所以我尝试添加航路点(Google 限制为 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
谢谢。