9/24/1-backLocation

This commit is contained in:
Hamza-Ayed
2024-09-24 13:32:36 +03:00
parent 3313cb0203
commit f5b7307f86
27 changed files with 880 additions and 343 deletions

View File

@@ -8,19 +8,13 @@ class LocationBackgroundController extends GetxController {
void onInit() {
super.onInit();
requestLocationPermission();
configureBackgroundLocation();
}
Future<void> requestLocationPermission() async {
var status = await Permission.locationAlways.status;
if (!status.isGranted) {
status = await Permission.locationAlways.request();
}
if (status.isGranted) {
configureBackgroundLocation();
} else {
// Handle permission denial
print("Location permission denied");
await Permission.locationAlways.request();
}
}
@@ -31,36 +25,29 @@ class LocationBackgroundController extends GetxController {
icon: '@mipmap/launcher_icon',
);
// Set the location update interval to 5 seconds
BackgroundLocation.setAndroidConfiguration(5000);
BackgroundLocation.setAndroidConfiguration(3000);
BackgroundLocation.startLocationService();
BackgroundLocation.getLocationUpdates((location) {
// Handle location updates here
print("Latitude: ${location.latitude}, Longitude: ${location.longitude}");
});
startBackLocation();
}
void startBackLocation() async {
Timer.periodic(const Duration(seconds: 5), (timer) async {
await getBackgroundLocation();
});
}
Future<void> getBackgroundLocation() async {
startBackLocation() async {
Timer.periodic(const Duration(seconds: 3), (timer) {
getBackgroundLocation();
});
}
getBackgroundLocation() async {
var status = await Permission.locationAlways.status;
if (status.isGranted) {
// The location service is already started in configureBackgroundLocation
// No need to call startLocationService again
BackgroundLocation.getLocationUpdates((location) {
// Handle location updates here
print(
"Latitude: ${location.latitude}, Longitude: ${location.longitude}");
});
await BackgroundLocation.startLocationService(
distanceFilter: 20, forceAndroidLocationManager: true);
BackgroundLocation.setAndroidConfiguration(
Duration.microsecondsPerSecond); // Set interval to 5 seconds
BackgroundLocation.getLocationUpdates((location1) {});
} else {
// Request permission if not granted
await Permission.locationAlways.request();
}
}