12/22/1
This commit is contained in:
@@ -1,21 +1,20 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:sefer_driver/controller/home/captin/home_captain_controller.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:google_maps_flutter/google_maps_flutter.dart';
|
||||
import 'package:location/location.dart';
|
||||
import 'package:sefer_driver/constant/box_name.dart';
|
||||
import 'package:sefer_driver/constant/links.dart';
|
||||
import 'package:sefer_driver/controller/functions/crud.dart';
|
||||
import 'package:sefer_driver/controller/home/payment/captain_wallet_controller.dart';
|
||||
import 'package:sefer_driver/main.dart';
|
||||
|
||||
import '../../constant/box_name.dart';
|
||||
import '../../constant/links.dart';
|
||||
import '../../main.dart';
|
||||
import '../../print.dart';
|
||||
import '../home/captin/home_captain_controller.dart';
|
||||
import '../home/payment/captain_wallet_controller.dart';
|
||||
import 'crud.dart';
|
||||
|
||||
// LocationController.dart
|
||||
class LocationController extends GetxController {
|
||||
LocationData? _currentLocation;
|
||||
late Location location;
|
||||
late Location location = Location();
|
||||
bool isLoading = false;
|
||||
late double heading = 0;
|
||||
late double accuracy = 0;
|
||||
@@ -28,7 +27,7 @@ class LocationController extends GetxController {
|
||||
late double speedAccuracy = 0;
|
||||
late double headingAccuracy = 0;
|
||||
bool isActive = false;
|
||||
late LatLng myLocation;
|
||||
late LatLng myLocation = LatLng(0, 0); // Default value
|
||||
String totalPoints = '0';
|
||||
LocationData? get currentLocation => _currentLocation;
|
||||
Timer? _locationTimer;
|
||||
@@ -36,13 +35,13 @@ class LocationController extends GetxController {
|
||||
@override
|
||||
void onInit() async {
|
||||
super.onInit();
|
||||
location = Location();
|
||||
getLocation();
|
||||
// startLocationUpdates();
|
||||
location = Location(); // Initialize the location object
|
||||
await getLocation(); // Fetch the location immediately
|
||||
startLocationUpdates(); // Start periodic location updates
|
||||
|
||||
totalPoints = Get.put(CaptainWalletController()).totalPoints.toString();
|
||||
// isActive = Get.put(HomeCaptainController()).isActive;
|
||||
} // Function to determine which area the coordinates belong to
|
||||
isActive = Get.put(HomeCaptainController()).isActive;
|
||||
}
|
||||
|
||||
String getLocationArea(double latitude, double longitude) {
|
||||
if (latitude >= 29.918901 &&
|
||||
@@ -67,6 +66,10 @@ class LocationController extends GetxController {
|
||||
|
||||
Future<void> startLocationUpdates() async {
|
||||
if (box.read(BoxName.driverID) != null) {
|
||||
if (location == null) {
|
||||
location = Location(); // Ensure location is initialized
|
||||
}
|
||||
|
||||
_locationTimer =
|
||||
Timer.periodic(const Duration(seconds: 5), (timer) async {
|
||||
try {
|
||||
@@ -77,10 +80,12 @@ class LocationController extends GetxController {
|
||||
if (isActive) {
|
||||
if (double.parse(totalPoints) > -300) {
|
||||
await getLocation();
|
||||
if (myLocation == null) {
|
||||
return;
|
||||
}
|
||||
print(
|
||||
'Latitude: ${myLocation.latitude}, Longitude: ${myLocation.longitude}');
|
||||
|
||||
// Determine the area
|
||||
String area =
|
||||
getLocationArea(myLocation.latitude, myLocation.longitude);
|
||||
print('Determined Area: $area');
|
||||
@@ -89,56 +94,25 @@ class LocationController extends GetxController {
|
||||
|
||||
switch (area) {
|
||||
case 'Cairo':
|
||||
print('Area matched: Cairo');
|
||||
box.write(BoxName.serverChosen, AppLink.seferCairoServer);
|
||||
endpoint = AppLink.addCarsLocationCairoEndpoint;
|
||||
break;
|
||||
case 'Giza':
|
||||
print('Area matched: Giza');
|
||||
box.write(BoxName.serverChosen, AppLink.seferGizaServer);
|
||||
endpoint = AppLink.addCarsLocationGizaEndpoint;
|
||||
break;
|
||||
case 'Alexandria':
|
||||
print('Area matched: Alexandria');
|
||||
box.write(
|
||||
BoxName.serverChosen, AppLink.seferAlexandriaServer);
|
||||
endpoint = AppLink.addCarsLocationAlexandriaEndpoint;
|
||||
break;
|
||||
default:
|
||||
print('Unknown location area. Fallback to Cairo');
|
||||
endpoint = AppLink.addCarsLocationCairoEndpoint;
|
||||
box.write(BoxName.serverChosen, AppLink.seferCairoServer);
|
||||
}
|
||||
|
||||
Log.print('Final Endpoint: $endpoint');
|
||||
switch (area) {
|
||||
case 'Cairo':
|
||||
box.write(BoxName.serverChosen, AppLink.seferCairoServer);
|
||||
endpoint = AppLink.addCarsLocationCairoEndpoint;
|
||||
Log.print('Endpoint: $endpoint');
|
||||
break;
|
||||
case 'Giza':
|
||||
box.write(BoxName.serverChosen, AppLink.seferGizaServer);
|
||||
endpoint = AppLink.addCarsLocationGizaEndpoint;
|
||||
Log.print('Endpoint: $endpoint');
|
||||
break;
|
||||
case 'Alexandria':
|
||||
box.write(
|
||||
BoxName.serverChosen, AppLink.seferAlexandriaServer);
|
||||
endpoint = AppLink.addCarsLocationAlexandriaEndpoint;
|
||||
Log.print('Endpoint: $endpoint');
|
||||
break;
|
||||
|
||||
default:
|
||||
// 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(),
|
||||
@@ -154,7 +128,6 @@ class LocationController extends GetxController {
|
||||
'status': box.read(BoxName.statusDriverLocation).toString(),
|
||||
});
|
||||
|
||||
// Update the camera position on the map
|
||||
Get.find<HomeCaptainController>()
|
||||
.mapHomeCaptainController
|
||||
?.animateCamera(
|
||||
@@ -169,7 +142,6 @@ class LocationController extends GetxController {
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
// Handle the error gracefully
|
||||
Log.print('Error during location update: $e');
|
||||
}
|
||||
});
|
||||
@@ -181,75 +153,58 @@ class LocationController extends GetxController {
|
||||
}
|
||||
|
||||
Future<void> getLocation() async {
|
||||
// isLoading = true;
|
||||
// update();
|
||||
if (location == null) {
|
||||
location = Location(); // Ensure location is initialized
|
||||
}
|
||||
|
||||
bool serviceEnabled;
|
||||
PermissionStatus permissionGranted;
|
||||
|
||||
// Check if location services are enabled
|
||||
serviceEnabled = await location.serviceEnabled();
|
||||
if (!serviceEnabled) {
|
||||
serviceEnabled = await location.requestService();
|
||||
if (!serviceEnabled) {
|
||||
// Location services are still not enabled, handle the error
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Check if the app has permission to access location
|
||||
permissionGranted = await location.hasPermission();
|
||||
if (permissionGranted == PermissionStatus.denied) {
|
||||
permissionGranted = await location.requestPermission();
|
||||
if (permissionGranted != PermissionStatus.granted) {
|
||||
// Location permission is still not granted, handle the error
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Configure location accuracy
|
||||
// LocationAccuracy desiredAccuracy = LocationAccuracy.high;
|
||||
|
||||
// Get the current location
|
||||
LocationData _locationData = await location.getLocation();
|
||||
myLocation =
|
||||
(_locationData.latitude != null && _locationData.longitude != null
|
||||
? LatLng(_locationData.latitude!, _locationData.longitude!)
|
||||
: null)!;
|
||||
getLocationArea(_locationData.latitude!, _locationData.longitude!);
|
||||
speed = _locationData.speed!;
|
||||
heading = _locationData.heading!;
|
||||
// Calculate the distance between the current location and the previous location
|
||||
if (_locationData.latitude != null && _locationData.longitude != null) {
|
||||
myLocation = LatLng(_locationData.latitude!, _locationData.longitude!);
|
||||
} else {
|
||||
myLocation = LatLng(0, 0); // Default value
|
||||
}
|
||||
|
||||
speed = _locationData.speed ?? 0;
|
||||
heading = _locationData.heading ?? 0;
|
||||
|
||||
if (Get.find<HomeCaptainController>().rideId == 'rideId') {
|
||||
Log.print(
|
||||
'Get.find<HomeCaptainController>().rideId: ${Get.find<HomeCaptainController>().rideId}');
|
||||
if (previousTime > 0) {
|
||||
double distance = calculateDistanceInKmPerHour(
|
||||
previousTime, _locationData.time, speed);
|
||||
totalDistance += distance;
|
||||
}
|
||||
|
||||
previousTime = _locationData.time!;
|
||||
previousTime = _locationData.time ?? 0;
|
||||
}
|
||||
// Print location details
|
||||
// isLoading = false;
|
||||
|
||||
update();
|
||||
}
|
||||
|
||||
double calculateDistanceInKmPerHour(
|
||||
double? startTime, double? endTime, double speedInMetersPerSecond) {
|
||||
// Calculate the time difference in hours
|
||||
double timeDifferenceInHours = (endTime! - startTime!) / 1000 / 3600;
|
||||
|
||||
// Convert speed to kilometers per hour
|
||||
double timeDifferenceInHours =
|
||||
(endTime ?? 0 - startTime! ?? 0) / 1000 / 3600;
|
||||
double speedInKmPerHour = speedInMetersPerSecond * 3.6;
|
||||
|
||||
// Calculate the distance in kilometers
|
||||
double distanceInKilometers = speedInKmPerHour * timeDifferenceInHours;
|
||||
|
||||
// Convert distance from kilometers to meters
|
||||
double distanceInMeters = distanceInKilometers * 1000;
|
||||
|
||||
// If the calculated distance is less than 6 meters, return 0 to avoid fake distance
|
||||
return distanceInMeters < 5 ? 0 : distanceInKilometers;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user