Skip to main content

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.



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

Post a Comment

Popular posts from this blog

Extent report plugin for cucumber framework

Extent Reports  are the most popular  reporting  used with Selenium. ExtentReport API makes our life easy to generate interactive  report  with simple configuartions. It supports almost all Java and .NET test frameworks such as TestNG , JUnit , NUnit etc Here we are discussing about  a plugin which is build on  Extent Report specially for Cucumber. This plugin is used to simple out the implementation of  Extent Report  in  Cucumber Framework .  We are creating a maven project to implement the integration of our plugin with cucumber 1. Create new maven project in any tool eclipse/sts/intellij 2. Open pom.xml and update below entries. Step 1 : Add Cucumber Extent Reporter library to Maven Project Add  cucumber-extentsreport <dependency>      <groupId> com.vimalselvam </groupId>      <artifactId> cucumber-extentsreport </artif...

java: You aren't using a compiler supported by lombok, so lombok will not work and has been disabled.

  In order to make projects compile with the existing builds of Lombok processor, as a workaround you can use the flag -Djps.track.ap.dependencies=false which should be added to File | Settings | Build, Execution, Deployment | Compiler | Build process VM options field. This will disable collection of dependencies specified by an annotation processor when Filer methods are called

Failure to transfer https://repo.maven.apache.org/maven2 was cached in the local repository

Error : Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:2.17 from https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced. Original error: Could not transfer artifact org.apache.maven.plugins:maven-surefire-plugin:pom:2.17 from/to central (https://repo.maven.apache.org/maven2): The operation was cancelled. pom.xml /LogReaderServer line 1 Maven Configuration Problem Solution : Remove all your failed downloads: For windows: cd %userprofile%\.m2\repository for /r %i in (*.lastUpdated) do del %i