I need to fetch the location in foreground and background in IOS Fluter.
I used the package background_location but it only work once, not recurring the process.
Future<void> checkAndRequestLocationPermission() async {
// Check the current location permission status
LocationPermission permission = await Geolocator.checkPermission();
// If permission is denied, request permission
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
}
// Handle permanently denied permissions
if (permission == LocationPermission.deniedForever) {
// Show a dialog or message to the user indicating that they need to enable location permissions in settings
print(
'Location permissions are permanently denied. Please enable them in settings.');
} else if (permission == LocationPermission.whileInUse ||
permission == LocationPermission.always) {
// Permission granted, proceed with location access
print('Location permission granted.');
if (Platform.isAndroid) {
await BackgroundLocation.setAndroidNotification(
title: "Notification title",
message: "Notification message",
icon: "@mipmap/ic_launcher",
);
await BackgroundLocation.setAndroidConfiguration(1000);
}
await BackgroundLocation.startLocationService(distanceFilter: 10)
.then((e) {
print(e.toString());
});
BackgroundLocation.getLocationUpdates((location) {
print(location);
});
} else {
// Handle other cases if needed
print('Location permission status: $permission');
}
}
Any idea, How to solve this.