The LocationService API allows you to use a mobile device’s native capabilities to determine a user’s current GPS location and to request location change updates in Lightning web components. There are many interesting use cases that can be implemented using this API, such as displaying a user’s location on a map, providing directional guidance for travel, calculating distances and travel time, or performing an action when a user approaches a location. In this blog post, we’ll discuss how to use this API to determine and track a user’s location.

Where to use the LocationService API

The LocationService API has been available for some time in Lightning apps distributed through Salesforce Mobile App Plus, Mobile Publisher for Experience Cloud, and the Field Service Mobile app. Now, it’s also available for the Android Salesforce Mobile App.

LocationService requires access to device hardware and device platform APIs. It does not use native device location capabilities when running in a standard web browser, whether the browser runs on a desktop or a mobile device. In this case, your code should fall back to using the browser location APIs.

Determining the current location

To determine a user’s current location using the LocationService API, you first import the JS module and check for service availability. If the service is unavailable, it means that the LWC is being used in an unsupported app or platform (typically an app running in a browser). In that case, you can fall back on other techniques, such as using the browser’s geolocation APIs, as we did in a component that we added to Dreamhouse.

Using the location, the component is able to calculate the distance from the user’s location to the selected property and show it to the user. This is what it looks like:

Property Distance component in Dreamhouse

See the example’s full code to learn more.

Tracking location changes

Another nice use case is the ability to subscribe to receive updates when a device’s location changes significantly. To do that, you instantiate the service in the same way as in the previous example, and then you subscribe to location updates as follows, providing a callback function to handle position updates when they happen.

Keep in mind that there’s also a watchPosition() fallback method (see docs) in the geolocation browser API that you can use when the native location service is not available.

Conclusion

In this blog post, you learned how to determine a user’s location and how to track location changes using the LocationService API and the user’s device capabilities. To learn more, see the full LocationService API documentation. Now that you are aware of this nice API, it’s time to think of use cases that might apply to your users and apps.

Also, be sure to check out other native device capabilities and APIs that are available for the different types of Salesforce mobile apps and review the recent blog posts we’ve published about some of them.

Native mobile capabilities and APIs available

About the author

Alba Rivas works as a Principal Developer Advocate at Salesforce. You can follow her on X or LinkedIn.

Get the latest Salesforce Developer blog posts and podcast episodes via Slack or RSS.

Add to Slack Subscribe to RSS