In my code in generate a route in segments and want to place a Marker near the centre of the leg and calculate total distance and time.
The problem comes if the user selects one of the alternate routes.
There appears to be no event documented that I can listen to detect the change in route to allow repositioning of the marker and update calculations.
I can use directions_changed to detect a dragged change of route but a change of route index through the user interface does not drive this event.
The route index is not even set prior to a call directions_changed a call to getRouteIndex returns undefined
(Chris Broadfoot noted this in June but unfortunately didn't post a solution)
Has anyone found out how to detect if an alternative route has been selected?
A website showing the problem is
http://calonmap.clanteam.com/CalOnMapPublic.html?&feedURL=https://www.google.com/calendar/feeds/calonmap%40gmail.com/public/fullClick on the car icons on map or sidebar and select an alternate route
the relevant code segment is:
// create Marker
var marker = new google.maps.Marker({
map: map,
position: spot,
value: l,
icon: travIcon,
title: title,
ZIndex: 1,
draggable: false
});
dirObj.Mark = marker;
var text = '<div>Directions leg '+l+'</div>'
// create info boxes for the marker
dirObj.infowindow = new google.maps.InfoWindow({
content: text
});
// add action events so the info window will be shown when the marker is clicked
google.maps.event.addListener(marker, 'click', function (){
var text = '<div id="directbox'+l+'"><b>'+dirObjArr[l].Title+'</b><div id="directboxdir'+l+'"></div></div>'
//print(text)
document.getElementById("event_list").innerHTML = text;
//document.write(text)
dirObjArr[l].Rend.setPanel(document.getElementById('directboxdir'+l));
dirObjArr[l].infowindow.setContent(document.getElementById('directbox'+l));
dirObjArr[l].infowindow.open(map, marker);
//print('directbox'+l)
});
}
// find and show route between the points
var request =
{
origin:locn[stIx],
destination:locn[endIx],
avoidHighways: avoidHighways,
avoidTolls: avoidTolls,
optimizeWaypoints: optWaypts,
provideRouteAlternatives: provideRouteAlternatives,
travelMode: travel,
unitSystem: unitSys
};
dirObj.Rend.setMap(map);
dirObj.Vis=false;
dirObj.Title = title;
dirObj.stIx = stIx;
dirObj.endIx = endIx;
dirObjArr[l]=dirObj;
dirObj.Serv.route(request,
function(response, status){
if (status == google.maps.DirectionsStatus.OK){
print('directs done '+l)
dirObjArr[l].Rend.setDirections(response);
var r = dirObjArr[l].Rend.getRouteIndex();
var midpoint = Math.floor(response.routes[r].legs[0].steps.length/2)
dirObjArr[l].Mark.setPosition(response.routes[r].legs[0].steps[midpoint].start_location);
}
else{procNoRoute(status);}
});