【发布时间】: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