【发布时间】:2013-10-21 23:28:07
【问题描述】:
我有一个应用程序可以跟踪车辆并在地图上绘制其行驶路径的折线。我想使用方向服务路由将此折线转换为路线。这将使我能够拖动路径并对其进行操作等。
问题是我想不出一个好的解决方案,而且我不确定这是否可能。如果我将折线的坐标数组传递给方向服务路线,它只使用折线的起点和终点绘制路线,它不会考虑中间的任何坐标。
我尝试使用折线坐标数组生成一个“航点”数组,方法是平均划分它并在其间获取 8 个坐标并将它们作为航点传递,但它现在根本无法渲染。如果我使用通过绘制路线生成的坐标数组来测试代码,它可以工作,所以我知道代码正在工作。我假设它失败了,因为其中一些坐标可能会稍微偏离道路(它是从 GPS 定位绘制的折线,所以它不是 100% 准确的),而且 Google 不只是将它捕捉到最近的接受位置。
谁能想到解决办法?
下面是代码示例,使其更清晰:
// In the polyline app
var encoded_path = google.maps.geometry.encoding.encodePath(coordinate_array)
// In the route app
var coordinates = google.maps.geometry.encoding.decodePath(encoded_path);
var waypoints = [];
// Evenly get coordinates across the entire array to be used as waypoints
for (var i = 1; i <= 8; ++i) {
var index = Math.floor((coordinates.length/10) * i);
if (index >= coordinates.length - 1)
break;
waypoints.push({
'location': new google.maps.LatLng(coordinates[index].lat(), coordinates[index].lng()),
'stopover': false
});
}
var request = {
origin: coordinates[0],
destination: coordinates[coordinates.length - 1],
travelMode: google.maps.DirectionsTravelMode.DRIVING,
waypoints: waypoints
};
MapService.directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
MapService.directionsDisplay.setDirections(response);
}
});
【问题讨论】:
-
我有一些想法,但我需要数据来测试。你能发布一个你正在使用的坐标数组吗?
-
你试过“optimizeWaypoints”标志吗?
-
我没有 Tomasz,但根据选项的描述,使用它并不理想,因为它会严重地重新安排路线。司机有时会采取非有效的方式来避开某些道路/桥梁等。乍得,抱歉,我找不到任何好的例子,我丢失了测试数据。
-
您是否将折线作为输入?您的应用程序没有其他输入吗?
标签: google-maps google-maps-api-3