I am using angular google maps in my angular Web App. I want to show directions from user's current location to a marker containing sub locations. In my web app the marker has multiple sub locations identified using lat and lng. So when user clicks on these sub locations, directions has to be shown from the user's current location.
I have installed agm-direction component and included in app-module.ts.
Currently, My HTML file has,
<agm-map [latitude]="latitude" [longitude]="longitude" [zoom]="zoom">
<agm-marker *ngFor="let marker of coordinates; let i = index"
[iconUrl]="icon"
[latitude]="marker.latitude"
[longitude]="marker.longitude"
[markerClickable]="true"
(markerClick)="markerClicked(marker.latitude,marker.longitude)">
<agm-snazzy-info-window [closeWhenOthersOpen]="true" [maxHeight]="300" [maxWidth]="1400">
<ng-template>
<mat-card>{{ pLocationId }} {{ pLocationName }}</mat-card><br/>
<mat-grid-list cols="12" rowHeight="100px" (ngModel)="pCycle">
<mat-grid-tile [colspan]="4" [rowspan]=2 *ngFor="let cycle of pCycle; let i = index" [style.background]="orange"
[style.padding-right]="20">
<mat-grid-tile-header [style.height]="30">{{i+1}}</mat-grid-tile-header>
<div [style.background]="orange">
<i class="material-icons">motorcycle</i>
<span [style.color]="white" >{{cycle.qrCode}}</span><br/>
<br/>
<i class="material-icons">battery_alert</i>
<span>{{ cycle.batteryLevel }}</span>
</div>
<mat-grid-tile-footer [style.background]="yellow">
<button mat-mini-fab (click)="showDirection(cycle.latitude, cycle.longitude)">
<mat-icon>navigation</mat-icon>
</button>
</mat-grid-tile-footer>
</mat-grid-tile>
</mat-grid-list>
</ng-template>
</agm-snazzy-info-window>
</agm-marker>
<agm-direction *ngIf="dir" [origin]="dir.origin" [destination]="dir.destination" [visible]="show"></agm-direction>
</agm-map>
And in TS file,
showDirection(dirLat: any, dirLng: any) {
let srcLat, srcLng;
if ('geolocation' in navigator) {
navigator.geolocation.getCurrentPosition((position) = > {
srcLat = position.coords.latitude;
srcLng = position.coords.longitude;
});
}
this.dir = {
origin: {
latitude: srcLat,
longitude: srcLng
},
destination: {
latitude: dirLat,
longitude: dirLng
}
};
this.show = true;
console.log('Nav button clicked');
}
But the expected behavior is not coming. The app is not showing directions and even the console output,
Nav button clicked
is also not coming. Please correct me where I am wrong.
NEW EDIT
Added <agm-direction>
tag inside the <agm-marker>
, now the console output is coming but direction is not showing.
Here is the new edited HTML,
<agm-map [latitude]="latitude" [longitude]="longitude" [zoom]="zoom">
<agm-marker *ngFor="let marker of coordinates; let i = index"
[iconUrl]="icon"
[latitude]="marker.latitude"
[longitude]="marker.longitude"
[markerClickable]="true"
(markerClick)="markerClicked(marker.latitude,marker.longitude)">
<agm-snazzy-info-window [closeWhenOthersOpen]="true" [maxHeight]="300" [maxWidth]="1400">
<ng-template>
<mat-card>{{ pLocationId }} {{ pLocationName }}</mat-card><br/>
<mat-grid-list cols="12" rowHeight="100px" (ngModel)="pCycle">
<mat-grid-tile [colspan]="4" [rowspan]=2 *ngFor="let cycle of pCycle; let i = index" [style.background]="orange"
[style.padding-right]="20">
<mat-grid-tile-header [style.height]="30">{{i+1}}</mat-grid-tile-header>
<div [style.background]="orange">
<i class="material-icons">motorcycle</i>
<span [style.color]="white" >{{cycle.qrCode}}</span><br/>
<br/>
<i class="material-icons">battery_alert</i>
<span>{{ cycle.batteryLevel }}</span>
</div>
<mat-grid-tile-footer [style.background]="yellow">
<button mat-mini-fab (click)="showDirection(cycle.latitude, cycle.longitude)">
<mat-icon>navigation</mat-icon>
</button>
</mat-grid-tile-footer>
</mat-grid-tile>
</mat-grid-list>
</ng-template>
</agm-snazzy-info-window>
<agm-direction *ngIf="dir" [origin]="dir.origin" [destination]="dir.destination" [visible]="show"></agm-direction>
</agm-marker>
</agm-map>