How do you show all alternative routes on a map

4,379 views
Skip to first unread message

dipens

unread,
Jan 5, 2011, 12:02:59 PM1/5/11
to google-map...@googlegroups.com
Hi,
 
I am calling the directions API to draw a route.
If I set provideRouteAlternatives: true, I want to display all the alternative routes that are returned.
 
I know that this has something to do with setting the route index - but am completely lost in how to do this.
I don't want to hard code anything and I want to show all the alternative routes as polylines (can be any number) that are returned.
 
How do I do this?
 
Thanks
map.js

geoco...@gmail.com

unread,
Jan 5, 2011, 2:07:54 PM1/5/11
to Google Maps JavaScript API v3
I think I have an example that displays alternate routes one at a time
using the directionRenderer (or there might be a google example that
does that), I don't know where mine is right now, if I have time to
find and post it I will.

I can think of 2 options:
1. make a directionRenderer for however many alternate routes are
returned and set each one to display one of the alternatives
2. process the DirectionsResult object, iterating through each
returned route and displaying it yourself.
(I have examples that do that to various levels of detail for
route[0])

-- Larry



>
> Thanks
>
>  map.js
> 1KViewDownload

dipens

unread,
Jan 10, 2011, 7:27:18 AM1/10/11
to Google Maps JavaScript API v3
Hi,

I have got this far:

http://www.differentum.com/map-test.html

As you can see, my alternative route is not working.
The route on the right should be starting at point A and not in
'Swanley'.

Does anyone know what I am doing wrong?
Just spent the whole of yesterday trying to resolve this issue.

Thanks
Dipen

On Jan 5, 7:07 pm, "geocode...@gmail.com" <geocode...@gmail.com>
wrote:
> On Jan 5, 9:02 am, dipens <goo...@differentum.com> wrote:
>
> > Hi,
>
> > I am calling the directions API to draw a route.
> > If I set provideRouteAlternatives: true, I want to display all the
> >alternativeroutesthat are returned.
>
> > I know that this has something to do with setting the route index - but am
> > completely lost in how to do this.
> > I don't want to hard code anything and I want to show all thealternative
> >routesas polylines (can be any number) that are returned.
>
> > How do I do this?
>
> I think I have an example that displays alternateroutesone at a time
> using the directionRenderer (or there might be a google example that
> does that), I don't know where mine is right now, if I have time to
> find and post it I will.
>
> I can think of 2 options:
> 1. make a directionRenderer for however many alternateroutesare
> returned and set each one to display one of the alternatives
> 2. process the DirectionsResult object, iterating through each
> returned route and displaying it yourself.
> (I have examples that do that to various levels of detail for
> route[0])
>
>   -- Larry
>
>
>
>
>
> > Thanks
>
> >  map.js
> > 1KViewDownload- Hide quoted text -
>
> - Show quoted text -

geoco...@gmail.com

unread,
Jan 10, 2011, 8:48:51 AM1/10/11
to Google Maps JavaScript API v3
On Jan 10, 4:27 am, dipens <goo...@differentum.com> wrote:
> Hi,
>
> I have got this far:
>
> http://www.differentum.com/map-test.html
>
> As you can see, my alternative route is not working.
> The route on the right should be starting at point A and not in
> 'Swanley'.
>
> Does anyone know what I am doing wrong?

Did you try either of my two suggestions? You are currently using the
same directions renderer to display all the routes, I wouldn't expect
that to work.

-- Larry

dipens

unread,
Jan 10, 2011, 8:52:28 AM1/10/11
to Google Maps JavaScript API v3
I thought I did - ok back to the drawing board...

On Jan 10, 1:48 pm, "geocode...@gmail.com" <geocode...@gmail.com>
> > > - Show quoted text -- Hide quoted text -

dipens

unread,
Jan 10, 2011, 9:56:46 AM1/10/11
to Google Maps JavaScript API v3
Hi Larry,

I've just updated:

http://www.differentum.com/map-test.html

For this test, I have commented out the lines in the 'for' loop and
have asked it to display the first alternative route (ie routeIndex =
1).

I am struggling with creating a new directionsRenderer object for each
route - as I thought I do this in my renderDirections function. I am
obviously missing something important!

Did you find any examples of how you display alternative routes?

Many Thanks
Dipen


On Jan 10, 1:52 pm, dipens <goo...@differentum.com> wrote:
> I thought I did - ok back to the drawing board...
>
> On Jan 10, 1:48 pm, "geocode...@gmail.com" <geocode...@gmail.com>
> wrote:
>
>
>
> > On Jan 10, 4:27 am, dipens <goo...@differentum.com> wrote:
>
> > > Hi,
>
> > > I have got this far:
>
> > >http://www.differentum.com/map-test.html
>
> > > As you can see, myalternativeroute is not working.
> > > The route on the right should be starting at point A and not in
> > > 'Swanley'.
>
> > > Does anyone know what I am doing wrong?
>
> > Did you try either of my two suggestions?  You are currently using the
> > same directions renderer to displayalltheroutes, I wouldn't expect
> > > > -Showquoted text -- Hide quoted text -
>
> > -Showquoted text -- Hide quoted text -
>
> -Showquoted text -

geoco...@gmail.com

unread,
Jan 10, 2011, 10:18:44 AM1/10/11
to Google Maps JavaScript API v3
On Jan 10, 6:56 am, dipens <goo...@differentum.com> wrote:
> Hi Larry,
>
> I've just updated:
>
> http://www.differentum.com/map-test.html
>
> For this test, I have commented out the lines in the 'for' loop and
> have asked it to display the first alternative route (ie routeIndex =
> 1).
>
> I am struggling with creating a new directionsRenderer object for each
> route - as I thought I do this in my renderDirections function. I am
> obviously missing something important!
>
> Did you find any examples of how you display alternative routes?

I haven't had a lot of time to look, and haven't found the example I
was looking for when I did look.

entering:
javascript:directionsRenderer.setRouteIndex(0);
in the browser address bar shows me the other route.
(FYI - there is something wrote with the route(1) in IE6)

-- Larry

geoco...@gmail.com

unread,
Jan 10, 2011, 10:44:21 AM1/10/11
to Google Maps JavaScript API v3
On Jan 10, 7:18 am, "geocode...@gmail.com" <geocode...@gmail.com>
wrote:
> On Jan 10, 6:56 am, dipens <goo...@differentum.com> wrote:
>
> > Hi Larry,
>
> > I've just updated:
>
> >http://www.differentum.com/map-test.html
>
> > For this test, I have commented out the lines in the 'for' loop and
> > have asked it to display the first alternative route (ie routeIndex =
> > 1).
>
> > I am struggling with creating a new directionsRenderer object for each
> > route - as I thought I do this in my renderDirections function. I am
> > obviously missing something important!
>
> > Did you find any examples of how you display alternative routes?
>
> I haven't had a lot of time to look, and haven't found the example I
> was looking for when I did look.
>
> entering:
> javascript:directionsRenderer.setRouteIndex(0);
> in the browser address bar shows me the other route.
> (FYI - there is something wrote with the route(1) in IE6)

You also have a problem with your polyline options object, it is not a
polyline options object, it is a polyline.

This works for me:
http://www.geocodezip.com/differentum_com_map-testA.html
> > - Show quoted text -- Hide quoted text -

dipens

unread,
Jan 10, 2011, 11:19:02 AM1/10/11
to Google Maps JavaScript API v3
Many Thanks Larry.
It works perfectly.

I really appreciate your help.

On Jan 10, 3:44 pm, "geocode...@gmail.com" <geocode...@gmail.com>
wrote:
> On Jan 10, 7:18 am, "geocode...@gmail.com" <geocode...@gmail.com>
> wrote:
>
>
>
>
>
> > On Jan 10, 6:56 am, dipens <goo...@differentum.com> wrote:
>
> > > Hi Larry,
>
> > > I've just updated:
>
> > >http://www.differentum.com/map-test.html
>
> > > For this test, I have commented out the lines in the 'for' loop and
> > > have asked it to display the firstalternativeroute (ie routeIndex =
> > > 1).
>
> > > I am struggling with creating a new directionsRenderer object for each
> > > route - as I thought I do this in my renderDirections function. I am
> > > obviously missing something important!
>
> > > Did you find any examples of how you displayalternativeroutes?
>
> > I haven't had a lot of time to look, and haven't found the example I
> > was looking for when I did look.
>
> > entering:
> > javascript:directionsRenderer.setRouteIndex(0);
> > in the browser address bar shows me the other route.
> > (FYI - there is something wrote with the route(1) in IE6)
>
> You also have a problem with your polylineoptionsobject, it is not a
> polylineoptionsobject, it is a polyline.

geoco...@gmail.com

unread,
Jan 10, 2011, 12:09:56 PM1/10/11
to Google Maps JavaScript API v3
On Jan 10, 8:19 am, dipens <goo...@differentum.com> wrote:
> Many Thanks Larry.
> It works perfectly.

Actually it doesn't. You have the code that initializes your map
inside your table.

Look again at my version of your code, you can only initialize the
map once the DOM has been completely rendered either by running it
when the onload function runs or at the very end of the page, just
before the </body> tag.

-- Larry

dipens

unread,
Jan 10, 2011, 1:15:33 PM1/10/11
to Google Maps JavaScript API v3
Thanks Larry.
I have moved it.

Much appreciated!
Dipen

On Jan 10, 5:09 pm, "geocode...@gmail.com" <geocode...@gmail.com>
> > - Show quoted text -- Hide quoted text -
Reply all
Reply to author
Forward
0 new messages