【发布时间】:2018-04-03 05:12:35
【问题描述】:
我在我的 Angular Web App 中使用 Angular 谷歌地图。我想显示从用户当前位置到包含子位置的标记的方向。在我的网络应用程序中,标记有多个使用 lat 和 lng 标识的子位置。因此,当用户点击这些子位置时,必须显示来自用户当前位置的方向。
我已经安装了 agm-direction 组件并包含在 app-module.ts 中。
目前,我的 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-marker>
<agm-direction *ngIf="dir" [origin]="dir.origin" [destination]="dir.destination" [visible]="show"></agm-direction>
</agm-map>
在 TS 文件中,
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');
}
但预期的行为并没有出现。该应用程序没有显示方向,甚至没有控制台输出,
点击导航按钮
也不来。 请纠正我的错误。
新编辑
在<agm-marker> 中添加了<agm-direction> 标签,现在控制台输出来了,但方向没有显示。
这是新编辑的 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>
【问题讨论】:
标签: angular angular-google-maps google-directions-api