AFGeofencing
Provides an interface for managing geofences with motion awareness and heartbeat.
This object allows starting and stopping geofencing services, and provides a Flow to observe geofence events. It utilizes Google Play Services for geofencing. It includes motion detection to adjust behavior and a heartbeat mechanism for reliability. A foreground service is used to maintain functionality when the app is in the background.
AFGeofencing is the main entry point for interacting with the geofencing functionality.
It provides methods to start and stop geofencing, and a flow to observe geofence events.
Example usage:
AFGeofencing.events.onEach { event ->
when (event) {
is AFGeofenceEvent.Enter -> println("Entered geofence: ${event.geofence.id}")
is AFGeofenceEvent.Exit -> println("Exited geofence: ${event.geofence.id}")
is AFGeofenceEvent.Error -> println("Geofencing error: ${event.throwable.message}")
else -> {}
}
}.launchIn(coroutineScope)
AFGeofencing.startGeofencing(listOf(
AFGeofence(id = "home", latitude = 37.7749, longitude = -122.4194, radius = 100f),
AFGeofence(id = "work", latitude = 37.7839, longitude = -122.4013, radius = 200f)
))Functions
Starts the geofencing service with the given list of geofences. It checks for permissions, Google Play Services availability, and location settings. It starts motion monitoring and a foreground service for reliability.
Stops the geofencing service. It removes all registered geofences, stops motion monitoring, the foreground service, and heartbeats.
Subscribe to AFGeofencing events using callbacks. Works fine from Swift and Objective-C without async/await.
Replaces the current set of monitored geofences with a new list. If geofencing is not active, this call is ignored.