startGeoFencing method
Start the geofencing service
Implementation
Future<void> startGeoFencing({
required double radiusMeter,
required int eventPeriodInSeconds,
double? lat,
double? lon,
}) async {
// Get current location if lat/lon are not provided
if (lat == null || lon == null) {
await getCurrentPosition();
}
// Create a GeoFirePoint from the current or provided coordinates
GeoFirePoint center = GeoFirePoint(
GeoPoint(lat ?? position!.latitude, lon ?? position!.longitude),
);
// Reference to Firestore collection where geofences will be stored
CollectionReference<Map<String, dynamic>> collectionReference =
FirebaseFirestore.instance.collection('geofences');
// Adding geofence data to Firestore
await collectionReference.add({
'position': center.data, // Store the geofence center
'radius': radiusMeter, // Store the geofence radius
'createdAt': FieldValue.serverTimestamp(), // Store the creation time
});
// Check geofence status stream
checkGeofenceStream(center, radiusMeter);
}