onGeofencesChange static method
Subscribe to changes in actively monitored geofences.
Fired when the list of monitored-geofences changed. The BackgroundGeolocation SDK contains powerful geofencing features that allow you to monitor any number of circular geofences you wish (thousands even), in spite of limits imposed by the native platform APIs (20 for iOS; 100 for Android).
The plugin achieves this by storing your geofences in its database, using a geospatial query to determine those geofences in proximity (@see Config.geofenceProximityRadius), activating only those geofences closest to the device's current location (according to limit imposed by the corresponding platform).
When the device is determined to be moving, the plugin periodically queries for geofences in proximity (eg. every minute) using the latest recorded location. This geospatial query is very fast, even with tens-of-thousands geofences in the database.
It's when this list of monitored geofences changes, that the plugin will fire the onGeofencesChange
event.
Note: For more information, see Geofencing Guide at GeofenceEvent.
Example
BackgroundGeolocation.onGeofencesChange((GeofencesChangeEvent event) {
List<String> on = event.on; //<-- new geofences activated.
List<Geofence> off = event.off; //<-- geofences that were just de-activated.
// Create map circles
on.forEach((Geofence geofence) {
createGeofenceMarker(geofence)
});
// Remove map circles
off.forEach((String identifier) {
removeGeofenceMarker(identifier);
}
});
The GeofencesChangeEvent provides only the changed geofences, those which just activated or de-activated.
When all geofences have been removed, the GeofencesChangeEvent will provide empty lists for both GeofencesChangeEvent.on & GeofencesChangeEvent.off.
Implementation
static void onGeofencesChange(Function(GeofencesChangeEvent) callback) {
if (_eventsGeofencesChange == null) {
_eventsGeofencesChange = _eventChannelGeofencesChange
.receiveBroadcastStream()
.map((dynamic event) =>
GeofencesChangeEvent(event['on'], event['off']));
}
_registerSubscription(_eventsGeofencesChange!.listen(callback), callback);
}