Use Location on a Mobile Device

A Lightning web component can use a mobile device’s location features to determine the current location of the device and, by association, the person who is holding it. You can access the device’s current location at a moment in time, or you can subscribe to location changes, and receive updates to the device’s location when it changes significantly.

Location is determined locally on the mobile device, and doesn’t need a network connection. LocationService does require access to platform-specific APIs that are available only within compatible Salesforce mobile apps.

LocationService does not and cannot function when running in a web browser, whether running on a desktop or mobile device.

Important

LocationService provides coordinate data only: latitude, longitude, altitude, and some motion details. It doesn’t include derived data, such as a physical address or map detail. If you’re using the location information to, for example, show a position on a map, you might need a network connection to receive map data, such as map tiles, and so on.

  • LocationService User Experience

    Your component can deliver any user experience you desire. There are a number of common location-based features where LocationService might be suitable.

  • Location Basics

    On the surface, the concept of location is a simple one. Where am I? Where is Salesforce Tower? How do I get to Salesforce Tower from where I am right now? These are all location-based features, and we’ve been using them on mobile devices for many years. As a developer, the concept of location can be more complex.

  • Use the LocationService API

    To develop a Lightning web component with location-based features, use the LocationService API to determine the current location.

  • 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.

  • Compatibility and Requirements

    LocationService is a JavaScript module that provides an API to Lightning web components to make mobile hardware and platform (operating system) features available in JavaScript. LocationService requires access to device hardware and device platform APIs. This access is only available when LocationService runs within a compatible Salesforce mobile app. It does not and cannot function when running in a standard web browser, whether the browser runs on a desktop or mobile device.

  • Considerations and Limitations

    Keep the following in mind when developing features that use the LocationService API.

See Also