This commit is contained in:
Hamza-Ayed
2023-09-25 02:48:18 +03:00
parent ebcc644d8c
commit 7290e5ecc7
8 changed files with 172 additions and 38 deletions

View File

@@ -8,37 +8,47 @@ import 'package:ride/constant/links.dart';
import 'package:ride/controller/functions/crud.dart';
import 'package:ride/main.dart';
import '../home/captin/home_captain_controller.dart';
// LocationController.dart
class LocationController extends GetxController {
LocationData? _currentLocation;
late Location location;
bool isloading = false;
LatLng mylocation = const LatLng(32.3, 36.3);
bool isLoading = false;
LatLng myLocation = const LatLng(32.3, 36.3);
LocationData? get currentLocation => _currentLocation;
Timer? _locationTimer;
@override
void onInit() {
void onInit() async {
super.onInit();
await CRUD().post(link: AppLink.addTokensDriver, payload: {
'token': box.read(BoxName.tokenDriver),
'captain_id': box.read(BoxName.driverID).toString()
});
location = Location();
getLocation();
startLocationUpdates();
}
void startLocationUpdates() async {
// if (Get.find<HomeCaptainController>().isActive) {
// Start the timer to get location every 20 seconds
_locationTimer = Timer.periodic(const Duration(seconds: 20), (timer) async {
await getLocation();
await CRUD().post(link: AppLink.addCarsLocationByPassenger, payload: {
'driver_id': box.read(BoxName.driverID).toString(),
'latitude': mylocation.latitude.toString(),
'longitude': mylocation.longitude.toString(),
'latitude': myLocation.latitude.toString(),
'longitude': myLocation.longitude.toString(),
});
});
// }
}
Future<void> getLocation() async {
isloading = true;
isLoading = true;
update();
bool serviceEnabled;
PermissionStatus permissionGranted;
@@ -68,7 +78,7 @@ class LocationController extends GetxController {
// Get the current location
LocationData _locationData = await location.getLocation();
mylocation =
myLocation =
(_locationData.latitude != null && _locationData.longitude != null
? LatLng(_locationData.latitude!, _locationData.longitude!)
: null)!;
@@ -78,7 +88,7 @@ class LocationController extends GetxController {
print('Latitude: ${_locationData.latitude}');
print('Longitude: ${_locationData.longitude}');
print('Time: ${_locationData.time}');
isloading = false;
isLoading = false;
update();
}
}