Reputation: 341
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?
Upvotes: 16
Views: 12301
Reputation: 71
I used this code for get way points, this code working properly
var rleg = directionsDisplay.directions.routes[0].legs[0];
// get way points from route
var w = [], wp;
wp = rleg.via_waypoints
for (var i = 0; i < wp.length; i++) {
w[i] = {
lat : wp[i].lat().toFixed(6),
lng : wp[i].lng().toFixed(6)
};
}
if (w.length !== 0) {
route.wayPoints = JSON.stringify(w);
} else {
console.log("no way points");
}
Upvotes: 0
Reputation: 3307
Yes as @sabotero point out, it is an expected behaviour, but there is nothing wrong or strange about that.
When optimizeWaypoints is true, google map try to find the best order of waypoints so that the distance be minimal. For example you can add four locations : Athens-NewYork-London-Los Angeles the google map will find that it is better to go first to London and after to New York. So will return in the array route.legs
Athens-London-NewYork-Los Angeles and in waypoint_order
will return [1, 0](is only two because is without origin and destination) to inform you that the original order of locations have change. That means that in the first position(waypoint_order[0]) will be the second(returns 1) location who is London and in the second position(waypoint_order[1]) will be the first location(returns 0) who is New York.
Note here that the origin and destination never change position, you always start from the origin you put and you end up in a destination you put. Thats why in waypoint_order is not appear the origin and the destination . So if you have same markers before the request and want to change the order you must exclude the origin and the destination when you reorder, this two will be in the same position.
Now if you want just to draw the markers from the request, don't use waypoint but route legs.
I had made an example, you can enable or disable the optimize with a checkbox and you can see that always the labels of markers come out with the right order: http://jsfiddle.net/TomKarachristos/03wtc6jv/
var marker = new google.maps.Marker({ //first,
position: route.legs[0].start_location,
map: map,
label: (0).toString()
});
for (var i = 1; i < route.legs.length; i++) {
var marker = new google.maps.Marker({
position: route.legs[i].start_location,
map: map,
label: i.toString()
});
}
var marker = new google.maps.Marker({ //last one
position: route.legs[route.legs.length - 1].end_location,
map: map,
label: route.legs.length.toString()
});
Upvotes: 4
Reputation: 10880
After spending a full working day on this, I finally figured out the mistake that I was making and it wasn't from the Google library.
I had to place the 1st marker by myself and then follow the waypoint_order
because the Array
that is supplied by waypoint_order
is for the waypoints which exclude the first/initial/origin position marker.
What I did was take my existing Array
of marker object and shift the first one and then sort the Array
according to the order given in waypoint_order
and then unshift() the first marker back to the markers Array. Unfortunately, I can't share the exact code because we have a wrapper over Google Maps and that code here would make no sense.
Another thing I ensured was not to place any marker on the map until I have already sorted according to the waypoint_order Array
.
Upvotes: 1
Reputation: 4365
Is an expected behaivour since:
If optimizeWaypoints was set to true, this field will contain the re-ordered permutation of the input waypoints. For example, if the input was:
Origin: Los Angeles
Waypoints: Dallas, Bangor, Phoenix
Destination: New York
and the optimized output was ordered as follows:
Origin: Los Angeles
Waypoints: Phoenix, Dallas, Bangor
Destination: New York
then this field will be an Array containing the values [2, 0, 1]. Note that the numbering of waypoints is zero-based.
If any of the input waypoints has stopover set to false, this field will be empty, since route optimization is not available for such queries.
As stated here: DirectionsResult
The waypoint_order is the optimized order
Upvotes: 14