addGeofence static method

Future<bool> addGeofence(
  1. Geofence geofence
)

Adds a Geofence to be monitored by the native Geofencing API.

If a Geofence already exists with the configured Geofence.identifier, the previous one will be deleted before the new one is inserted.

See also addGeofences for adding multiple geofences.

Example

BackgroundGeolocation.addGeofence(Geofence(
  identifier: "Home",
  radius: 150,
  latitude: 45.51921926,
  longitude: -73.61678581,
  notifyOnEntry: true,
  notifyOnExit: false,
  notifyOnDwell: true,
  loiteringDelay: 30000,  // 30 seconds
  extras: {               // Optional arbitrary meta-data
    zone_id: 1234
  }
)).then((bool success) {
  print('[addGeofence] success');
}.catchError((error) {
  print('[addGeofence] FAILURE: ${error}');
});

Note:

  • When adding a list-of-geofences, it's about 10 times faster to use addGeofences instead.
  • See GeofenceEvent for more information.

Implementation

static Future<bool> addGeofence(Geofence geofence) async {
  return (await _methodChannel.invokeMethod<bool>(
      'addGeofence', geofence.toMap())) as FutureOr<bool>;
}