I have a map with a route (several destinations - not only A, B, as on the page below). Directions are being dragged. I would like to save the modified routes. The page below is what I was going to ...
http://vikku.info/programming/google-maps-v3/draggable-directions/saving-draggable-directions-saving-waypoints-google-directions-google-maps-v3.htm
Here is what I have:
google.maps.event.addListener(directionsDisplay, 'directions_changed', function() {
console.log('reroute');
var rleg_count = directionsDisplay.directions.routes[0].legs.length;
data.start = {
'lat':directionsDisplay.directions.routes[0].legs[0].start_location.lat(),
'lng':directionsDisplay.directions.routes[0].legs[0].start_location.lng()
};
data.end = {
'lat':directionsDisplay.directions.routes[0].legs[rleg_count-1].end_location.lat(),
'lng':directionsDisplay.directions.routes[0].legs[rleg_count-1].end_location.lng()
};
var wp=[];
var w = [];
var route = directionsDisplay.directions.routes[0];
for (var l = 0; l < route.legs.length; l++)
{
for(var j = 0; j < route.legs[l].via_waypoints.length; j++)
{
w.push({
location:{'lat':route.legs[l].via_waypoints[j].lat(), 'lng':route.legs[l].via_waypoints[j].lng()},
stopover:true
});
}
}
data.waypoints = w;
});
And the SetRoute function:
function setRoute(os)
{
var wp = [];
for(var i=0;i<os.waypoints.length;i++)
{
wp[i] = {
'location': new google.maps.LatLng(os.waypoints[i].location.lat, os.waypoints[i].location.lng),
'stopover': os.waypoints[i].stopover
}
}
var request = {
'origin':new google.maps.LatLng(os.start.lat, os.start.lng),
'destination':new google.maps.LatLng(os.end.lat, os.end.lng),
'waypoints': wp,
optimizeWaypoints: false,
avoidHighways: false,
avoidTolls: false,
travelMode: google.maps.TravelMode.DRIVING,
}
directionsService.route(request, function(res,sts){
if(sts=='OK')
directionsDisplay.setDirections(res);
});
}
The problem is that the code works if there are only 2 destinations (A, B), but as soon as there are more destinations ... it does not work correctly ... it ignores other destinations
As far as I can tell, waypoints are correct. There seems to be a problem with multiple addresses, and I don't know how to fix it.
Thank!