Implementation #
You may want to be aware of users entering and leaving the geofences of your building. In order to listen to these interactions you must implement the GeofenceListener interface / delegate (Android, iOS). With this interface you will be notified with a list of Geofences(Android, iOS) when the user enters or leaves any of them:
private GeofenceListener geofenceListener = new GeofenceListener() { @Override public void onEnteredGeofences(List<Geofence> enteredGeofences) { Log.d(TAG, "onEnteredGeofences: "); } @Override public void onExitedGeofences(List<Geofence> exitedGeofences) { Log.d(TAG, "onExitedGeofences: "); } };
// onEnterGeofences() will return an array with the geofences that the user enters. // See https://github.com/situmtech/cordova?tab=readme-ov-file#--onentergeofences cordova.plugins.Situm.onEnterGeofences((enteredGeofences: any) => { let namesArray: string[] = []; enteredGeofences.forEach((geofence: any) => { let geofenceNameWithId = `${geofence.name}(${geofence.identifier})`; namesArray = [...namesArray, geofenceNameWithId]; }); console.log(`onEnterGeofences: ${namesArray}`); }); // onExitGeofences() will return an array with the geofences that the user exits. // See https://github.com/situmtech/cordova?tab=readme-ov-file#--onexitgeofences cordova.plugins.Situm.onExitGeofences((exitedGeofences: any) => { let namesArray: string[] = []; exitedGeofences.forEach((geofence: any) => { let geofenceNameWithId = `${geofence.name}(${geofence.identifier})`; namesArray = [...namesArray, geofenceNameWithId]; }); console.log(`onExitGeofences: ${namesArray}`); });
// onEnterGeofences() will return an array with the geofences that the user enters. // See https://developers.situm.com/sdk_documentation/react-native/typedoc/classes/default.html#onEnterGeofences SitumPlugin.onEnterGeofences((enteredGeofences: any) => { let namesArray: string[] = []; enteredGeofences.forEach((geofence: any) => { let geofenceNameWithId = `${geofence.name}(${geofence.identifier})`; namesArray = [...namesArray, geofenceNameWithId]; }); console.log(`onEnterGeofences: ${namesArray}`); }); // onExitGeofences() will return an array with the geofences that the user exits. // See https://developers.situm.com/sdk_documentation/react-native/typedoc/classes/default.html#onExitGeofences SitumPlugin.onExitGeofences((exitedGeofences: any) => { let namesArray: string[] = []; exitedGeofences.forEach((geofence: any) => { let geofenceNameWithId = `${geofence.name}(${geofence.identifier})`; namesArray = [...namesArray, geofenceNameWithId]; }); console.log(`onExitGeofences: ${namesArray}`); });
In order to start using this listener you must attach it to our SDK with the following line of code:
// Make sure you have initialized our SDK with SitumSdk.init(Context) SitumSdk.locationManager().setGeofenceListener(geofenceListener);
// In Cordova this step is not required, we will send you updates as soon as you implement onEnterGeofences() & onExitGeofences() callbacks
// In React Native this step is not required, we will send you updates as soon as you implement onEnterGeofences() & onExitGeofences() callbacks
In case you want to stop this listener, do the following:
SitumSdk.locationManager().setGeofenceListener(null);
cordova.plugins.Situm.onEnterGeofences(null); cordova.plugins.Situm.onExitGeofences(null);
SitumPlugin.onEnterGeofences(null); SitumPlugin.onExitGeofences(null);
Remember to set this listener before the user starts positioning, it won’t work if you attach it while positioning.
Take in cosideration #
- This listener will only work with SDK 2.80.0 onwards.
- Positioning geofences (with trainer_metadata custom field) will be ignored by these callbacks. As this type of geofences are used to get better positioning in uncalibrated areas, the entries and exits inside them won’t be notified.
- These callbacks only work with indoor locations. Any outdoor location will produce a call to onExitedGeofences with the last positioned geofences list as parameter.