2

according to this, directionRenderer object's method setRouteIndex() should Set the (zero-based) index of the route in the DirectionsResult object to render.

here is what i have done:

directionsService.route(request, function(response, status) {
                    if (status == google.maps.DirectionsStatus.OK) {
                        directionsDisplay.setRouteIndex(1);
                        console.log(directionsDisplay.getRouteIndex());
                        directionsDisplay.setDirections(response);
                        console.log(directionsDisplay.getRouteIndex());

} });

i know there are 3 alternative routes the query i am doing with. here, first console.log(directionsDisplay.getRouteIndex()); gives route no. 1, thats okay , but after the excecution of setDirections, the secong log gives the value 0!. that means the first route. i have enabled, alternative routes, provideRouteAlternatives:true, i also initiated the object like this:

var rendererOptions = {
     routeIndex:1 
    }
directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);

But it still shows the route no. 0, not number 1. here is my webpage, please do check the source. thanks in advance.

1 Answers1

1

I think you are misunderstanding something. You said:

console.log(directionsDisplay.getRouteIndex()); gives route no. 1, thats okay , but after the excecution of setDirections, the secong log gives the value 0!.

...and that's OK too because you are resetting it with: directionsDisplay.setDirections(response);

but you don't need this line because the response is already loaded into the DirectionsRenderer object.

geocodezip
  • 158,664
  • 13
  • 220
  • 245
Marcelo
  • 9,387
  • 3
  • 35
  • 40
  • thanks, probably i am not getting it, what i think setDirection is doing, getting the result from directionsService.route and drawing the route in the map. if i comment that line out, it doesnt, draw the route in the map, how would i draw the route now, without setDirections. – Ataur Rahim Chowdhury Aug 30 '12 at 10:10
  • thanks, i am trying this, i would get new route using getDirections(), then i am gonna pass it on setDirections. – Ataur Rahim Chowdhury Aug 30 '12 at 10:12
  • See the example from the docs: https://google-developers.appspot.com/maps/documentation/javascript/examples/directions-complex – Marcelo Aug 30 '12 at 10:25
  • okay i left google api as it is, solved it with age old swapping! 0 to temp, 1 to 0, temp to 1! :D – Ataur Rahim Chowdhury Aug 30 '12 at 11:11
  • 2
    I had this problem and the solution that worked for me was to call setRouteIndex() after setDirections() is called. This seems unintuitive to me because you would ideally change the route before the route is drawn and the map is rendered, but in practice it seems to work fine and you see the route you want without the first route being rendered and then hidden. My application needs to select the route with the shortest distance, but Google usually selects the route with the shortest journey time by default and the wrong route gets shown. – Noel Whitemore Nov 19 '15 at 22:59