Develop Secure Code
LocationService Data Types
getLocationService()
isAvailable()
getCurrentPosition(options)
startWatchingPosition(options, callback)
stopWatchingPosition(watchId)
getCurrentPosition(options)Gets the device’s current geolocation.
1import { getLocationService } from 'lightning/mobileCapabilities';
2getCurrentPosition(options: LocationServiceOptions): Promise<Object>options—(Required) A LocationServiceOptions object to configure the location request.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.
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