我有一个应用程序,可以跟踪车辆并在地图上绘制它们的路径。我想使用路线指示服务将此折线转换为路线。这将允许我拖动路径并进行操作等。
问题是我无法想到一个好的解决方案,并且我不确定是否可能。如果我将折线的坐标数组传递给路线指示服务,它只会使用折线的起点和终点来绘制路线,而不考虑中间的任何坐标。
我尝试使用折线坐标数组生成“途经点”数组,通过平均分割它并获取8个坐标,然后将其作为途经点传递,但现在它完全无法呈现。 如果我使用绘制路线生成的坐标数组测试代码,则代码有效,因此我知道代码是有效的。我假设失败的原因是其中某些坐标可能略微偏离道路(它是从GPS定位绘制的折线,因此不是100%准确),而Google不会将其捕捉到最近的接受位置。
有人能想到解决方法吗?
以下是一些代码示例,以使问题更清晰:
问题是我无法想到一个好的解决方案,并且我不确定是否可能。如果我将折线的坐标数组传递给路线指示服务,它只会使用折线的起点和终点来绘制路线,而不考虑中间的任何坐标。
我尝试使用折线坐标数组生成“途经点”数组,通过平均分割它并获取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);
}
});