Search code examples
google-mapsgoogle-maps-api-3map-directionsgoogle-directions-api

setRouteIndex not working in google map direction JS api v3


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.


Solution

  • 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.