and*_*o1d 14 google-maps google-maps-api-3
我有一个应用程序跟踪车辆并在地图上绘制他们的旅行路线的折线.我想使用路线服务路线将此折线转换为路线.这将允许我能够拖动路径并操纵它等.
问题是我想不出一个很好的解决方案,我不确定它是否可行.如果我将折线的坐标数组传递到路线服务路线,它只使用折线的起点和终点绘制路线,而不考虑其间的任何坐标.
我尝试使用折线坐标数组生成一个'waypoints'数组,方法是将它平均分割并在两者之间获得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);
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2809 次 |
| 最近记录: |