GeofencingService User Experience
Use the GeofencingService API
GeofencingService Example
Compatibility and Requirements
Considerations and Limitations
Previous Versions
To develop a Lightning web component with location-based features for creating and monitoring geofences, use the GeofencingService API.
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:
1import { getGeofencingService } from "lightning/mobileCapabilities";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.
1handleClickAddGeofencingRegion(event) {
2 const myGeofencingService = getGeofencingService();
3 if(myGeofencingService.isAvailable()) {
4 // Perform geofencing operations
5 }
6 else {
7 // GeofencingService not available
8 // Not running on hardware with location APIs, etc.
9 // Handle with message, error, beep, and so on
10 }
11}Use the StartMonitoringGeofence() function to start monitoring a geofence region. Then, handle the outcome in whatever manner you wish.
1sftowerEntry = {
2 latitude: 37.7899,
3 longitude: -122.3969,
4 radius: 50,
5 notifyOnEntry: true,
6 message: “Welcome to Salesforce Tower”,
7 triggerOnce: false
8}
9
10addGeofence() {
11 // …
12 if(myGeofencingService.isAvailable()) {
13 // Perform geofencing operations
14 myGeofencingService.startMonitoringGeofence(this.sftowerEntry)
15 .then((id) => {
16 console.log(`ID for geofence SF Tower Entry created: ${id}`);
17 })
18 .catch((error) => {
19 console.log(error);
20 });
21 }
22}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.
1handleClickRemoveGeofencingRegion(geofenceRegionId) {
2 // …
3 if(myGeofencingService.isAvailable()) {
4 // Perform geofencing operations
5 myGeofencingService.stopMonitoringGeofence(geofenceRegionId)
6 .then(() => {
7 console.log(`Success`);
8 })
9 .catch((error) => {
10 console.log(error);
11 });
12 }
13}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.
1handleClickRemoveGeofencingRegion(geofenceRegionId) {
2 // …
3 if(myGeofencingService.isAvailable()) {
4 // Perform geofencing operations
5 myGeofencingService.stopMonitoringGeofence(geofenceRegionId)
6 .then(() => {
7 console.log(`Success`);
8 })
9 .catch((error) => {
10 console.log(error);
11 });
12 }
13}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.
1handleClickGetAllGeofenceIDs() {
2 // …
3 if(myGeofencingService.isAvailable()) {
4 // Perform geofencing operations
5 myGeofencingService.getMonitoredGeofences()
6 .then((results) => {
7 const activeGeofences = JSON.parse(JSON.stringify(results));
8 const msg = `Number of geofences: + ${activeGeofences.length}`;
9 console.log(msg);
10 })
11 .catch((error) => {
12 console.log(error);
13 });
14 }
15}See Also