I'm developing a system to trace route using Google Maps API. I have the points of origin and destination and between these points there are some waypoints. By tracing the route Google returns me the best route and mark these points on the map. We display the route data in a div. My function that calculates the route, the part that returns the data looks like this:
directionsService.route(request, $.proxy(function(response, status){
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
var orders = response.routes[0].waypoint_order;
var route = response.routes[0];
var total_distance = 0;
var displayRoute = $('#detail-route');
for (i = 0; i < route.legs.length; i++){
var routeSegment = i + 1,
from_address = route.legs[i].start_address.split(','),
to_address = route.legs[i].end_address.split(',');
total_distance += Math.floor(route.legs[i].distance.value / 1000);
displayRoute.append('<b>Trecho ' + routeSegment + ': </b><br/>');
displayRoute.append('<b>Saindo de: </b>' + from_address[0] + '<br/>');
displayRoute.append('<b>Indo para: </b>' + to_address[0] + '<br/>');
displayRoute.append('<b>Distância: </b>' + route.legs[i].distance.text);
}
displayRoute.prepend('total:' + this.format(total_distance) + ' km' + '<br/><br/>');
function format()
is my function for format km..
The problem is that, on some routes, the waypoint_order
shows a different order than the one in legs
. For example:
For a given route, the route.legs[i]
returns order: 'waypoint 0, waypoint 1, waypoint 3, waypoint 2', but the waypoint_order
attribute returns [3, 0, 2, 1, 3]
Is this the expected behavior, or am i missing something here?
[3, 0, 1, 3]
or is that a typo on your part? i.e. it's missing waypoint #2? Or did you mean to type something like[3, 0, 1, 2]
? – duncan Dec 9 '13 at 9:56google.maps.DirectionsRequest
object, please? – sabotero Dec 9 '13 at 15:55