How to detect change in directions RouteIndex?

1,618 views
Skip to first unread message

MymsMan

unread,
Aug 18, 2011, 3:40:14 PM8/18/11
to google-map...@googlegroups.com
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/full
Click 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);}
                          });

MymsMan

unread,
Aug 20, 2011, 5:38:27 AM8/20/11
to google-map...@googlegroups.com
No suggestions?

I don't want to have to implement my own route selection interface to get around this problem.
I will probably file a bug/feature request on the issues tracker since this doesn't appear to have been reported.

xelawho

unread,
Sep 14, 2011, 4:53:30 PM9/14/11
to Google Maps JavaScript API v3
> No suggestions?

dunno if you solved this one yet, but this helped me with what sounds
like a similar problem:

google.maps.event.addListener(directionsDisplay, 'directions_changed',
function() {

infowindow1.setPosition(directionsDisplay.dragResult.routes[0].legs[0].steps[0].start_location);
});

MymsMan

unread,
Sep 14, 2011, 5:31:32 PM9/14/11
to google-map...@googlegroups.com
I haven't solved the problem - I have a directions_changed event listener but it doesn't get driven when the user selects one of the suggested alternative routes so I can't move my marker or update my total route length/time totals.

xelawho

unread,
Sep 14, 2011, 6:00:41 PM9/14/11
to Google Maps JavaScript API v3
> it doesn't get driven when the user selects one of the suggested
> alternative routes so I can't move my marker or update my total route
> length/time totals.

yes, I was having that problem (or something like it) until I put the
listener inside the response callback. Or at least that's what I think
you call where it is... so in your example it would look something
like this:


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)

google.maps.event.addListener(dirObjArr[l], 'directions_changed',
function() {

dirObjArr[l].Mark.setPosition(dirObjArr[l].Rend.dragResult.routes[r].legs[0].steps[midpoint].start_location);
});


or something.

hope that helps.

MymsMan

unread,
Sep 15, 2011, 8:03:32 AM9/15/11
to google-map...@googlegroups.com
Thanks for your suggestion but moving the definition of the directions_changed listener didn't make any difference.

The problem is the listener is Broken As Designed - the specification says :-
"This event is fired when the rendered directions change, either when a new DirectionsResult is set or when the user finishes dragging a change to the directions path."
it doesn't mention a change to an alternate route

I would argue that selecting an alternate route is the equivalent to a change in directions and the listener should be driven but that is not what is documented/Implemented :(

Without an event being driven there is no way to detect when the user chooses one of the alternate routes.

I have raised issue 3565 but no response yet, http://code.google.com/p/gmaps-api-issues/issues/detail?id=3565

xelawho

unread,
Sep 15, 2011, 9:52:55 AM9/15/11
to Google Maps JavaScript API v3
> Thanks for your suggestion but moving the definition of the
> directions_changed listener didn't make any difference.

yes, you're absolutely right - I was getting confused between an
alternate route chosen by dragging the directions path and one chosen
by clicking on one of the suggestions given. I've made a slightly
simpler demonstration of the problem here:
http://xelawho.com/map/dirpanel.htm

and as you say, choosing an alternate route from the sidebar doesn't
even fire an alert that the directions have changed (while dragging it
does).

I've "starred" the issue, for what it's worth.

MymsMan

unread,
Sep 15, 2011, 10:09:23 AM9/15/11
to google-map...@googlegroups.com
Many thanks for providing the simple example - my code is growing excessively large and complex! :-)

MymsMan

unread,
Dec 22, 2011, 8:46:33 AM12/22/11
to google-map...@googlegroups.com
From Maps API office hours I discover there actually is a routeindex_changed event but this is not documented.

So the issue should be changed to please ensure that ALL events are documented in the API reference,

Users are not mind readers and with the closure compilers it is difficult to be a code reader!

It is frustrating that I have suffered this problem for over 4 months and been unable to find the answer before now despite raising an issue and writing forum posts
Reply all
Reply to author
Forward
0 new messages