Agm-Direction icon indicating copy to clipboard operation
Agm-Direction copied to clipboard

Alternative routes are not displayed

Open chris248-git opened this issue 5 years ago • 5 comments

Since the directive is providing the option "provideRouteAlternatives" it does not display the alternative routes.

Within the code of the directive only the first route is called --> see line 180: const _route = response.routes[0].legs[0];

According to the directions api description, google is providing an array of alternative routes.

chris248-git avatar Dec 25 '19 10:12 chris248-git

Hi @chris248-git

According to the Directions Requests, alternative routes won't show at the same time.

If you want to get the result of alternative routes, you can set [provideRouteAlternatives]="true" and use (onResponse) to listening the result.

Alternative routes response:

image

Or using the option panel :
<agm-map [latitude]="lat" [longitude]="lng">
  <agm-direction
   [origin]="origin"
   [destination]="destination"
   [provideRouteAlternatives]="true"
   (onResponse)="onResponse($event)"
   [panel]="myPanel"
   >
  </agm-direction>
</agm-map>
<div #myPanel></div>
  public lat = 24.799448;
  public lng = 120.979021;

  public origin: any = 'No. 1, Section 1, Taiwan Avenue, Central District, Taichung, Taiwan';
  public destination: any = 'No. 1 Guanqian Road, North District, Taichung City';

  public onResponse(event: any) {
    console.log(event);
    // You can do anything.
  }

Finally, the code in line 180 const _route = response.routes[0].legs[0]; won't affect alternative routes. Only pick up the origin's and destination's location and address.

explooosion avatar Jan 05 '20 15:01 explooosion

Thanks a lot. I managed to draw also the alternative routes using a for loop in the onResponse method:

public onResponse(event: any) {
    let color: string[] = ['#FF6859', '#FFCF44', '#B15DFF']     

    for (let i = 0, len = event.routes.length; i < len; i++) {

      this.myDir[i] = new google.maps.DirectionsRenderer({
            map: this.thisMap,
            directions: event,
            routeIndex: i,
            polylineOptions: { strokeColor: color[i] }
        });
    }   
}

How can I remove this directions if I'd like to get a new destination?

chris24802 avatar Jan 18 '20 11:01 chris24802

@explooosion Can you show an example for selecting an alternate route using the draggable option ?

jnair-tsystems avatar Aug 05 '20 10:08 jnair-tsystems

hi @chris248-git @chris24802, can you post a most complete snippet of your alternative route implementation, especially how you achieve select the agm map component stored in "this.thisMap" and what's "this.myDir" and why you have to store the render. Tnx in advance

peploc-innocv avatar Oct 29 '20 14:10 peploc-innocv

Hi All, please help on this issue. i need show alternative routes on map like google map

ashokpal100 avatar Nov 29 '21 13:11 ashokpal100