This commit is contained in:
Hamza-Ayed
2024-08-31 09:09:11 +03:00
parent d23020188e
commit 8a9d270672
10 changed files with 387 additions and 121 deletions

View File

@@ -45,33 +45,24 @@ class LocationController extends GetxController {
} // Function to determine which area the coordinates belong to
String getLocationArea(double latitude, double longitude) {
// Giza Boundary Check
if (latitude >= 29.904975 &&
latitude <= 30.143372 &&
longitude >= 30.787030 &&
longitude <= 31.238843) {
// box.write(BoxName.serverChosen, AppLink.seferGizaServer);
return 'Giza';
}
// Cairo Boundary Check
else if (latitude >= 29.918901 &&
if (latitude >= 29.918901 &&
latitude <= 30.198857 &&
longitude >= 31.215009 &&
longitude <= 31.532186) {
// box.write(BoxName.serverChosen, AppLink.seferCairoServer);
return 'Cairo';
}
// Alexandria Boundary Check
else if (latitude >= 30.396286 &&
} else if (latitude >= 29.904975 &&
latitude <= 30.143372 &&
longitude >= 30.787030 &&
longitude <= 31.238843) {
return 'Giza';
} else if (latitude >= 30.396286 &&
latitude <= 31.654458 &&
longitude >= 29.041139 &&
longitude <= 32.626259) {
// box.write(BoxName.serverChosen, AppLink.seferAlexandriaServer);
return 'Alexandria';
} else {
return 'Outside';
}
// Return 'Unknown' if outside defined areas
return 'Unknown';
}
Future<void> startLocationUpdates() async {
@@ -82,27 +73,54 @@ class LocationController extends GetxController {
totalPoints =
Get.find<CaptainWalletController>().totalPoints.toString();
isActive = Get.find<HomeCaptainController>().isActive;
if (isActive) {
if (double.parse(totalPoints) > -3000) {
await getLocation();
// Determine the area based on current location
String area =
getLocationArea(myLocation.latitude, myLocation.longitude);
print('Determined Area: $area');
String endpoint;
switch (
getLocationArea(myLocation.latitude, myLocation.longitude)) {
switch (area) {
case 'Cairo':
endpoint = AppLink.addCarsLocationCairoEndpoint;
box.write(BoxName.serverChosen, AppLink.seferCairoServer);
Log.print('Endpoint: $endpoint');
break;
case 'Giza':
endpoint = AppLink.addCarsLocationGizaEndpoint;
Log.print('Endpoint: $endpoint');
box.write(BoxName.serverChosen, AppLink.seferGizaServer);
break;
case 'Alexandria':
endpoint = AppLink.addCarsLocationAlexandriaEndpoint;
Log.print('Endpoint: $endpoint');
box.write(
BoxName.serverChosen, AppLink.seferAlexandriaServer);
break;
case 'Outside':
// Handle cases outside of Cairo, Giza, and Alexandria
print('Location outside Cairo, Giza, or Alexandria');
endpoint = AppLink
.addCarsLocationCairoEndpoint; // Fallback to Cairo endpoint
Log.print('Fallback Endpoint: $endpoint');
box.write(BoxName.serverChosen, AppLink.seferCairoServer);
break;
default:
print('Location outside Cairo, Giza, or Alexandria');
endpoint = AppLink.addCarsLocationByPassenger;
// Handle any other unexpected cases
print('Unknown location area');
endpoint = AppLink
.addCarsLocationCairoEndpoint; // Fallback to Cairo endpoint
Log.print('Fallback Endpoint: $endpoint');
box.write(BoxName.serverChosen, AppLink.seferCairoServer);
return;
}
// Ensure driver ID exists before making the API call
if (box.read(BoxName.driverID) != null) {
await CRUD().post(link: endpoint, payload: {
'driver_id': box.read(BoxName.driverID).toString(),
@@ -115,46 +133,26 @@ class LocationController extends GetxController {
: totalDistance < 1
? totalDistance.toStringAsFixed(3)
: totalDistance.toStringAsFixed(1),
'status': box.read(BoxName.statusDriverLocation).toString()
'status': box.read(BoxName.statusDriverLocation).toString(),
});
// Animate camera to user location (optional)
// if (Get.find<HomeCaptainController>().rideId == 'rideId') {
// Get.find<MapDriverController>()
// .mapController!
// .animateCamera(CameraUpdate.newLatLng(LatLng(
// Get.find<LocationController>().myLocation.latitude,
// Get.find<LocationController>().myLocation.longitude,
// )));
// Update the camera position on the map
Get.find<HomeCaptainController>()
.mapHomeCaptainController
?.animateCamera(
CameraUpdate.newLatLng(
LatLng(
Get.find<LocationController>().myLocation.latitude,
Get.find<LocationController>().myLocation.longitude,
),
),
);
}
Get.find<HomeCaptainController>()
.mapHomeCaptainController!
.animateCamera(CameraUpdate.newLatLng(LatLng(
Get.find<LocationController>().myLocation.latitude,
Get.find<LocationController>().myLocation.longitude,
)));
// if (Get.find<HomeCaptainController>().rideId == '0') {
// await sql.insertData({
// 'driver_id': box.read(BoxName.driverID),
// 'latitude': myLocation.latitude.toString(),
// 'longitude': myLocation.longitude.toString(),
// 'created_at': DateTime.now().toString(),
// }, TableName.carLocations);
// } else {
// await sql.insertData({
// 'order_id': Get.find<MapDriverController>().rideId,
// 'created_at': DateTime.now().toString(),
// 'lat': myLocation.latitude.toString(),
// 'lng': myLocation.longitude.toString(),
// }, TableName.rideLocation);
// }
}
//
}
// }
} catch (e) {
// Handle the error gracefully
Log.print('Error during location update: $e');
}
});
}