getCurrentPosition(options)

Gets the device’s current geolocation.

Syntax 

1import { getLocationService } from 'lightning/mobileCapabilities';
2getCurrentPosition(options: LocationServiceOptions): Promise<Object>

Parameters 

Returns 

A Promise object that resolves as a Location object with the device location details.

A rejected promise returns a LocationServiceFailure object containing details about the error.

Usage 

This function performs a one-time location request, and returns the device’s current location at that time.

1myLocationService
2  .getCurrentPosition({ enableHighAccuracy: true })
3  .then((result) => {
4    this.myLocation = result.coords;
5  })
6  .catch((error) => {
7    console.error(error);
8  });

When this function is called, a request to get the current location is submitted to the device’s location capabilities. When the device’s current location is determined, the promise resolves with a Location object. Determining the current location can take varying lengths of time, from nearly instant to many seconds, depending on a number of external factors.

There’s no user interface associated with a location request. To provide feedback while waiting for a response, update your Lightning web component code. We suggest that you display a lightning-spinner just before calling getCurrentPosition(). Then remove the spinner in the finally() clause of the getCurrentPosition() promise handler.

See Also