import 'dart:async'; import 'package:get/get.dart'; import 'package:google_maps_flutter/google_maps_flutter.dart'; import 'package:location/location.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'; class LocationController extends GetxController { LocationData? _currentLocation; late Location location = Location(); bool isLoading = false; late double heading = 0; late double accuracy = 0; late double previousTime = 0; late double latitude; late double totalDistance = 0; late double longitude; late DateTime time; late double speed = 0; late double speedAccuracy = 0; late double headingAccuracy = 0; bool isActive = false; late LatLng myLocation = LatLng(0, 0); // Default value String totalPoints = '0'; LocationData? get currentLocation => _currentLocation; Timer? _locationTimer; @override void onInit() async { super.onInit(); 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; } String getLocationArea(double latitude, double longitude) { if (latitude >= 29.918901 && latitude <= 30.198857 && longitude >= 31.215009 && longitude <= 31.532186) { return 'Cairo'; } else if (latitude >= 29.904975 && latitude <= 30.143372 && longitude >= 30.787030 && longitude <= 31.215009) { return 'Giza'; } else if (latitude >= 30.396286 && latitude <= 31.654458 && longitude >= 29.041139 && longitude <= 32.626259) { return 'Alexandria'; } else { return 'Cairo'; } } Future 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 { totalPoints = Get.find().totalPoints.toString(); isActive = Get.find().isActive; if (isActive) { if (double.parse(totalPoints) > -300) { await getLocation(); if (myLocation == null) { return; } print( 'Latitude: ${myLocation.latitude}, Longitude: ${myLocation.longitude}'); String area = getLocationArea(myLocation.latitude, myLocation.longitude); print('Determined Area: $area'); String endpoint; switch (area) { case 'Cairo': box.write(BoxName.serverChosen, AppLink.seferCairoServer); endpoint = AppLink.addCarsLocationCairoEndpoint; break; case 'Giza': box.write(BoxName.serverChosen, AppLink.seferGizaServer); endpoint = AppLink.addCarsLocationGizaEndpoint; break; case 'Alexandria': box.write( BoxName.serverChosen, AppLink.seferAlexandriaServer); endpoint = AppLink.addCarsLocationAlexandriaEndpoint; break; default: endpoint = AppLink.addCarsLocationCairoEndpoint; box.write(BoxName.serverChosen, AppLink.seferCairoServer); } Log.print('Final Endpoint: $endpoint'); if (box.read(BoxName.driverID) != null) { await CRUD().post(link: endpoint, payload: { 'driver_id': box.read(BoxName.driverID).toString(), 'latitude': myLocation.latitude.toString(), 'longitude': myLocation.longitude.toString(), 'heading': heading.toString(), 'speed': (speed * 3.6).toStringAsFixed(1), 'distance': totalDistance == 0 && (speed * 3.6) < 5 ? '0.0' : totalDistance < 7 ? totalDistance.toStringAsFixed(3) : totalDistance.toStringAsFixed(1), 'status': box.read(BoxName.statusDriverLocation).toString(), }); Get.find() .mapHomeCaptainController ?.animateCamera( CameraUpdate.newLatLng( LatLng( Get.find().myLocation.latitude, Get.find().myLocation.longitude, ), ), ); } } } } catch (e) { Log.print('Error during location update: $e'); } }); } } void stopLocationUpdates() { _locationTimer?.cancel(); } Future getLocation() async { if (location == null) { location = Location(); // Ensure location is initialized } bool serviceEnabled; PermissionStatus permissionGranted; serviceEnabled = await location.serviceEnabled(); if (!serviceEnabled) { serviceEnabled = await location.requestService(); if (!serviceEnabled) { return; } } permissionGranted = await location.hasPermission(); if (permissionGranted == PermissionStatus.denied) { permissionGranted = await location.requestPermission(); if (permissionGranted != PermissionStatus.granted) { return; } } LocationData _locationData = await location.getLocation(); 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().rideId == 'rideId') { if (previousTime > 0) { double distance = calculateDistanceInKmPerHour( previousTime, _locationData.time, speed); totalDistance += distance; } previousTime = _locationData.time ?? 0; } update(); } double calculateDistanceInKmPerHour( double? startTime, double? endTime, double speedInMetersPerSecond) { double timeDifferenceInHours = (endTime ?? 0 - startTime! ?? 0) / 1000 / 3600; double speedInKmPerHour = speedInMetersPerSecond * 3.6; double distanceInKilometers = speedInKmPerHour * timeDifferenceInHours; double distanceInMeters = distanceInKilometers * 1000; return distanceInMeters < 5 ? 0 : distanceInKilometers; } }