LocationService Example

Here’s a basic example of a Lightning web component that gets the user’s current location and displays it on a map.

The HTML template provides the bare minimum for a location-based interface. There’s an element to display the map, a bit of static help text, and a button to get the location. There are two interesting aspects of this template:

  • Disabling the Get Current Location button using the disabled attribute when not in a supported Salesforce mobile app. This attribute is set based on the results of isAvailable() when the component is initialized.
  • A spinner that indicates “indeterminate progress” while waiting for the current location request to resolve.

Once the current location is determined, we use the lightning-map base component to display it. Each phase of the location request lifecycle writes a console message.

See Also