Tuesday 12 March 2019

Directions and Waypoints in google map using AGM map angular 6




If you have not Setup agm map till now then check this post Google map in angular 6 



First Install NPM direction module in your project.


npm i @agm/core
npm i agm-direction



Import direction module in app.component.ts :

Importing Modules
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
 
import { AgmCoreModule } from '@agm/core';            // @agm/core
import { AgmDirectionModule } from 'agm-direction';   // agm-direction
 
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AgmCoreModule.forRoot({ // @agm/core
      apiKey: 'your key',
    }),
    AgmDirectionModule,     // agm-direction
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }


Open the file src/app/app.component.ts and paste the following content:

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.css'],
})
export class AppComponent {
  title: string = 'Google Map';
  lat: number = 51.678418;
  lng: number = 7.809007;
  public origin: { lat: 51.673858, lng: 7.815982 };
  public destination: { lat: 51.723858, lng: 7.895982 };
  waypoints = [
    {
      location: { lat: 51.673858, lng: 7.815982 },
      stopover: true
    },
    {
      location: { lat: 51.373858, lng: 7.215982 },
      stopover: true
    },
    {
      location: { lat: 51.723858, lng: 7.895982 },
      stopover: true
    }
  ]
}


Open the file src/app/app.component.html and paste the following content:

app.component.html :

<agm-map [latitude]="lat" [longitude]="lng">
  <agm-direction [origin]="origin" [destination]="destination" 
  [waypoints]="waypoints" [travelMode]="'DRIVING'">
</agm-direction>
</agm-map>


This will create direction and also add your waypoints in the direction.



2 comments:

  1. Error: src/app/locationmarker/locationmarker.component.html:3:5 - error TS2322: Type '{ location: { lat: number; lng: number; }; stopover: boolean; }[]' is not assignable to type 'DirectionsWaypoint[]'.
    Type '{ location: { lat: number; lng: number; }; stopover: boolean; }' is not assignable to type 'DirectionsWaypoint'.
    Types of property 'location' are incompatible.
    Type '{ lat: number; lng: number; }' is not assignable to type 'string | Place | LatLng | undefined'.
    Type '{ lat: number; lng: number; }' is missing the following properties from type 'LatLng': equals, toUrlValue, toJSON

    3 [waypoints]="waypoints" [travelMode]="'DRIVING'">
    ~~~~~~~~~~~~~~~~~~~~~~~

    src/app/locationmarker/locationmarker.component.ts:8:16
    8 templateUrl: './locationmarker.component.html',
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Error occurs in the template of component LocationmarkerComponent.
    src/app/locationmarker/locationmarker.component.html:3:29 - error TS2322: Type '"DRIVING"' is not assignable to type 'TravelMode | undefined'.

    3 [waypoints]="waypoints" [travelMode]="'DRIVING'">
    ~~~~~~~~~~~~~~~~~~~~~~~~

    src/app/locationmarker/locationmarker.component.ts:8:16
    8 templateUrl: './locationmarker.component.html',
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Error occurs in the template of component LocationmarkerComponent.



    m facing this issue

    ReplyDelete
  2. make sure u install all dependencies mentioned in the post.
    And also import them as needed

    ReplyDelete

Spring boot with CORS

CORS (Cross-Origin Resource Sharing) errors occur when a web application running in a browser requests a resource from a different domain or...