using .setRouteIndex to render alternate path

908 views
Skip to first unread message

bmcf

unread,
Sep 20, 2010, 5:31:49 AM9/20/10
to Google Maps JavaScript API v3
I would like to have the map show alternate routes on demand. It
initially shows the first route in the "result" by default (there are
three routes). I tried calling .setRouteIndex(1) and
then .setDirections(result) on the DirectionsRenderer object but the
second route was not rendered on the map. Is this not the way to do
it?

Thanks.

bmcf

unread,
Sep 20, 2010, 10:52:01 PM9/20/10
to Google Maps JavaScript API v3
I should have made it clear that I am not using the .setPanel method
on the DirectionsRenderer, I am trying to handle the display of
directions with my own function.

stu

unread,
Sep 21, 2010, 6:33:52 AM9/21/10
to Google Maps JavaScript API v3
Hi
I'd guess that if you are rendering the results to the map yourself,
that you can't then use DirectionsRenderer to get to routes.

I presume you are getting the results object and either using the
overview_path or going through the legs, steps etc to build the
polyline,
In that case you *can* use the result object which has an array of the
routes (as long as the DirectionsRequest has provideRouteAlternatives
set to true)
if your DirectionsRequest result is called directionsResult, then you
just ask for the routes like
directionsResult.routes[n] where n is the index for one of the
routes.

To get your basic polyline path for route number 2:
directionsResult.routes[1].overview_path

Does that help?
...stu

bmcf

unread,
Sep 21, 2010, 1:19:40 PM9/21/10
to Google Maps JavaScript API v3
No, I am using the DirectionsRenderer to display the routes on the
map, but I am handling the display of instuctions with my own
function. The basic problem seems to be that calling setRouteIndex(1)
or setRouteIndex(2) on the DirectionsRenderer object before calling
setDirections(result) has no effect -- the DirectionsRenderer still
renders route 0 on the map, even though calling getRouteIndex shows
that the route index has been set to 1 or 2 rather than 0. I am able
to display the instructions for the alternate paths by accessing the
alternate routes in the result object, but I can't get
DirectionsRenderer to render the alternate paths on the map.

Your answer will be helpful if I have to resort to rendering the paths
on the map myself.
> > > Thanks.- Hide quoted text -
>
> - Show quoted text -

geoco...@gmail.com

unread,
Sep 21, 2010, 2:03:39 PM9/21/10
to Google Maps JavaScript API v3
On Sep 21, 10:19 am, bmcf <brian.mcf...@gmail.com> wrote:
> No, I am using the DirectionsRenderer to display the routes on the
> map, but I am handling the display of instuctions with my own
> function. The basic problem seems to be that calling setRouteIndex(1)
> or setRouteIndex(2) on the DirectionsRenderer object before calling
> setDirections(result) has no effect -- the DirectionsRenderer still
> renders route 0 on the map, even though calling getRouteIndex shows
> that the route index has been set to 1 or 2 rather than 0. I am able
> to display the instructions for the alternate paths by accessing the
> alternate routes in the result object, but I can't get
> DirectionsRenderer to render the alternate paths on the map.

Do you have an example that you can post a link to where it isn't
working?

Seems to work for me.

-- Larry
> > - Show quoted text -- Hide quoted text -

bmcf

unread,
Sep 21, 2010, 4:03:16 PM9/21/10
to Google Maps JavaScript API v3
Larry,

My example is here:

http://www.brianmcfann.com/examples/example.htm

I am setting the route index directly by assigning to
directionsDisplay.routeIndex rather than calling
directionsDisplay.setRouteIndex() because the latter seems not to work
when I use a variable or function that resolves to a number (perhaps I
am not understanding something about how javascript and/or jquery
works in this context). Also, in my experiments, it seems that
setRouteIndex() will only set the index once and will cause the script
to fail if I try to reset it.

Thanks,

Brian

On Sep 21, 11:03 am, "geocode...@gmail.com" <geocode...@gmail.com>
wrote:

geoco...@gmail.com

unread,
Sep 21, 2010, 4:37:51 PM9/21/10
to Google Maps JavaScript API v3
On Sep 21, 1:03 pm, bmcf <brian.mcf...@gmail.com> wrote:
> Larry,
>
> My example is here:
>
> http://www.brianmcfann.com/examples/example.htm
>
> I am setting the route index directly by assigning to
> directionsDisplay.routeIndex rather than calling
> directionsDisplay.setRouteIndex() because the latter seems not to work
> when I use a variable or function that resolves to a number (perhaps I
> am not understanding something about how javascript and/or jquery
> works in this context). Also, in my experiments, it seems that
> setRouteIndex() will only set the index once and will cause the script
> to fail if I try to reset it.

Why are you rendering the directions again after setting the
routeIndex? That sets it back to 0 (I suspect).

If I comment out this line in the renderDirections alternate-route
change function:
// renderDirections(directionsDisplay.directions);

this works:
directionsDisplay.setRouteIndex(parseInt($('#alternate-
route').attr('value')));
(it doesn't like it if I leave out the "parseInt"...)

-- Larry

bmcf

unread,
Sep 22, 2010, 11:09:44 PM9/22/10
to Google Maps JavaScript API v3
Thanks, Larry, that works. To answer your question, I didn't know that
setting the route index was sufficient to have the alternate path
rendered. Thank you for pointing that out to me.

Brian

On Sep 21, 1:37 pm, "geocode...@gmail.com" <geocode...@gmail.com>

Praveen C. Naik

unread,
Jun 25, 2017, 7:34:29 PM6/25/17
to Google Maps JavaScript API v3

i have the same exact problem , but your code is in jquery and mine is pure Javascript , so can i get some help here..!
Reply all
Reply to author
Forward
0 new messages