Angular 16 Get Current Location Latitude and Longitude

Angular 16 gets the current location latitude and longitude; Through this tutorial, i am going to show you how to get the current location latitude and longitude in Angular 16 using the google AGM core package.

Angular 16 Get Current Location Latitude and Longitude

Use the following steps to integrate Google Maps in the Angular 16 aps; as follows:

Step 1 – Create Angular Application

Run the following command on command prompt to create a new angular application by executing the below command:

$ ng new angular-google-map-app

Then, move inside the application directory:

$ cd angular-google-map-app

Step 2 – Install AGM/Core Package

Then run the following command on command prompt to install the AGM code library package by executing the below npm command:

$ npm install @agm/core@1 --save

Then run the following command on command prompt to install the type for the google maps library. So, execute below npm type package as well:

$ npm install @types/googlemaps --save-dev

Thereafter, open the file, then add the "googlemaps" under the types array property:

  "extends": "./tsconfig.base.json",
  "compilerOptions": {
    "outDir": "./out-tsc/app",
    "types": [
  "files": [
  "include": [

Step 3 – Update App Module with AgmCore Module

Import the AgmCoreModule then add into the imports array. After that, we will be able to use Google maps in our Angular application.

Google API Key: You also need to specify the Google API key in the App Module. You need to visit the Google Console, then create the API key.

Let’s open the app.module.ts file then update it as shown below:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { FormsModule } from '@angular/forms';
import { AgmCoreModule } from '@agm/core';
  declarations: [
  imports: [
      apiKey: 'GOOGLE API KEY',
      libraries: ['places']
  providers: [],
  bootstrap: [AppComponent]
export class AppModule { }

Step 4 – Update Component Class

Now, define the variable to keep latitude and longitude values. Head towards the app.component.ts file and update the following code:

import { Component, OnInit, ViewChild, ElementRef, NgZone } from '@angular/core';
import { MapsAPILoader } from '@agm/core';
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
export class AppComponent implements OnInit {
  title: string = 'AGM project';
  latitude!: number;
  longitude!: number;
  public searchElementRef!: ElementRef;
    private mapsAPILoader: MapsAPILoader,
    private ngZone: NgZone
  ) { }
  ngOnInit() {
    //load Places Autocomplete
    this.mapsAPILoader.load().then(() => {
  onMapClicked(event: any){
    this.latitude =;
    this.longitude = event.coords.lng;

Above the onMapClicked event will be triggered on clicking on the map on any area to return the Object carrying latitude, longitude and other information.

Step 5 – Update HTML Template

Add the following code into the app.component.html; as follows:

<!-- app.component.html -->
<div class="container">
  <h1>Angular Google Maps - Get Latitude and Longitude on Click</h1>
  <agm-map [latitude]="latitude" [longitude]="longitude" (mapClick)="onMapClicked($event)">
  <div>Latitude: {{latitude}}</div>
  <div>Longitude: {{longitude}}</div>

The (mapClick) event will return the current coordinates on clicking on google map.

Step 6 – Run Application

Finally, run the application by hitting the following command:

$ ng serve --open

It will the application at following URL


Recommended Angular Tutorials

Leave a Comment