Use the GeofencingService API
To develop a Lightning web component with location-based features for creating and monitoring geofences, use the GeofencingService API.
- Import GeofencingService to make the GeofencingService API functions available to your code.
- Test to make sure GeofencingService is available before you call geofencing functions.
- Use the geofencing functions to get the current location and to define regions of interest.
In your component’s JavaScript file, import GeofencingService using the standard JavaScript import statement. Specifically, import the getGeofencingService() factory function from the lightning/mobileCapabilities module, like so:
After it’s imported into your component, use the factory function to get an instance of GeofencingService. With your GeofencingService instance, use the utility functions and constants to verify availability. Then use the geofencing function to create location arrival or departure notifications for geographic regions.
GeofencingService depends on physical device hardware and platform features. A component that uses GeofencingService renders without errors on a desktop computer or in a mobile browser, but geofencing functions fail. To avoid these errors, test whether GeofencingService functionality is available before you use it.
Use the StartMonitoringGeofence() function to start monitoring a geofence region. Then, handle the outcome in whatever manner you wish.
Use the StopMonitoringAllGeofences() function to stop monitoring all geofence regions. Callback is called when the monitoring stops for all geofence regions, or with an error if the monitoring fails to stop.
Use the StopMonitoringAllGeofences() function to stop monitoring all geofence regions. Callback is called when the monitoring stops for a geofence region, or with an error if the monitoring fails to stop.
Use the getMonitoredGeofences() function to get the IDs of active geofence regions. Provide a callback function to handle all IDs of geofences being monitored, or with an error if the query fails.
See Also