我有一个跟踪车辆并在地图上绘制出行程路线折线的应用程序。我想使用路线服务路线将此折线转换为路线。这将允许我能够拖动路径和操纵它等。将折线转换为路线
问题是我想不出一个很好的解决方案,我不知道是否有可能。如果我将多段线的坐标数组传递给方向服务路线,则它只会使用多段线的起点和终点绘制一条路线,而不考虑其间的任何坐标。
我试图生成使用折线一个“航点”阵列由均匀划分它和在之间并在消逝那些作为沿途停车点获得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”标志? –
我没有托马斯,但根据选项的描述,它不会很理想,因为它会严重重新排列路线。司机有时会采取非效率的方式来避开某些道路/桥梁等。 乍得,抱歉,但我找不到任何好的例子给你,我失去了测试数据。 – andro1d