How to get current location in Angular 14+

How to get current location in Angular 14+

In this article, we will see how to get the user's current location in Angular 14+.

Generate location service

ng g service services/location/location

It will create location service inside services folder

Now add below code in it

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

  providedIn: 'root',
export class LocationService {
  constructor() {}

  getCurrentLocation() {
    return new Promise((resolve, reject) => {
      if (navigator.geolocation) {
          (position) => {
            if (position) {
                'Latitude: ' +
                  position.coords.latitude +
                  'Longitude: ' +
              let lat = position.coords.latitude;
              let lng = position.coords.longitude;

              const location = {
          (error) => console.log(error)
      } else {
        reject('Geolocation is not supported by this browser.');

Now use this location service in app.component.ts file as below:

 const position: any = await this.locationService.getCurrentLocation();

You will get the current latitude and longitude on the console.

Subscribe to our Newsletter

Stay up to date! Get all the latest posts delivered straight to your inbox.

If You Appreciate What We Do Here On TutsCoder, You Should Consider:

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Leave a Comment