npm i @agm/core
npm i agm-direction
app.module.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { FormsModule } from '@angular/forms'; import { AppComponent } from './app.component'; import { AgmCoreModule } from '@agm/core'; import { AgmDirectionModule} from 'agm-direction'; // agm-direction @NgModule({ imports: [ BrowserModule, FormsModule, AgmCoreModule.forRoot({ //@agm/core apiKey:'' }), AgmDirectionModule //agm-direction ], declarations: [ AppComponent ], bootstrap: [ AppComponent ] }) export class AppModule { } |
app.component.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import { Component } from '@angular/core'; import { MouseEvent } from '@agm/core'; @Component({ selector: 'my-app', templateUrl: './app.component.html', styleUrls: [ './app.component.css' ] }) export class AppComponent { name = 'Angular 5'; //initial center position lat: Number = 24.799448; lng: Number = 120.979021; //google maps zoom zoom: Number = 14; //Get Directions dir = undefined; public getDirection() { this.dir = { origin: { lat: 24.799448, lng: 120.979021 }, destination: { lat: 24.799524, lng: 120.975017 } } } } |
app.component.html
1 2 3 4 5 6 7 |
<h1>Angular Google Maps Direction (agm-direction) TEST</h1> <agm-map [latitude]="lat" [longitude]="lng"> <agm-direction *ngIf="dir" [origin]="dir.origin" [destination]="dir.destination"></agm-direction> </agm-map> <button type="button" (click)="getDirection()">Get</button |
app.component.css
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
p { font-family: Lato; } agm-map { height: 400px; } * { font-family: Lato; } a { color: darkblue; } |
FULL SOURCE CODE