25-5-30/1
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import 'dart:async';
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:get/get.dart';
|
||||
import 'package:google_maps_flutter/google_maps_flutter.dart';
|
||||
@@ -7,7 +8,6 @@ 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';
|
||||
@@ -18,64 +18,37 @@ class LocationController extends GetxController {
|
||||
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
|
||||
late LatLng myLocation = LatLng(0, 0);
|
||||
String totalPoints = '0';
|
||||
LocationData? get currentLocation => _currentLocation;
|
||||
Timer? _locationTimer;
|
||||
|
||||
LatLng? _lastSavedPosition;
|
||||
|
||||
@override
|
||||
void onInit() async {
|
||||
super.onInit();
|
||||
location = Location(); // Initialize the location object
|
||||
location = Location();
|
||||
await location.changeSettings(
|
||||
accuracy: LocationAccuracy.high,
|
||||
interval: 5000, // 5 seconds
|
||||
distanceFilter: 0);
|
||||
await getLocation(); // Fetch the location immediately
|
||||
await startLocationUpdates(); // Start periodic location updates
|
||||
accuracy: LocationAccuracy.high, interval: 5000, distanceFilter: 0);
|
||||
await getLocation();
|
||||
await startLocationUpdates();
|
||||
|
||||
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';
|
||||
// }
|
||||
// }
|
||||
String getLocationArea(double latitude, double longitude) {
|
||||
final locations = box.read(BoxName.locationName) ?? [];
|
||||
for (final location in locations) {
|
||||
final locationData = location as Map<String, dynamic>;
|
||||
|
||||
// Debugging: Print location data
|
||||
|
||||
// Convert string values to double
|
||||
final minLatitude =
|
||||
double.tryParse(locationData['min_latitude'].toString()) ?? 0.0;
|
||||
final maxLatitude =
|
||||
@@ -85,21 +58,15 @@ class LocationController extends GetxController {
|
||||
final maxLongitude =
|
||||
double.tryParse(locationData['max_longitude'].toString()) ?? 0.0;
|
||||
|
||||
// Debugging: Print converted values
|
||||
// 'Converted Values: minLatitude=$minLatitude, maxLatitude=$maxLatitude, minLongitude=$minLongitude, maxLongitude=$maxLongitude');
|
||||
|
||||
if (latitude >= minLatitude &&
|
||||
latitude <= maxLatitude &&
|
||||
longitude >= minLongitude &&
|
||||
longitude <= maxLongitude) {
|
||||
box.write(BoxName.serverChosen,
|
||||
EncryptionHelper.instance.decryptData(locationData['server_link']));
|
||||
// 'locationData----server_link: ${locationData['server_link']}');
|
||||
box.write(BoxName.serverChosen, (locationData['server_link']));
|
||||
return locationData['name'];
|
||||
}
|
||||
}
|
||||
|
||||
// Default case
|
||||
box.write(BoxName.serverChosen, AppLink.seferCairoServer);
|
||||
return 'Cairo';
|
||||
}
|
||||
@@ -108,10 +75,6 @@ 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 {
|
||||
@@ -121,7 +84,7 @@ class LocationController extends GetxController {
|
||||
|
||||
if (isActive && double.parse(totalPoints) > -300) {
|
||||
await getLocation();
|
||||
if (myLocation == null) return;
|
||||
if (myLocation.latitude == 0 && myLocation.longitude == 0) return;
|
||||
|
||||
String area =
|
||||
getLocationArea(myLocation.latitude, myLocation.longitude);
|
||||
@@ -132,22 +95,35 @@ class LocationController extends GetxController {
|
||||
'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),
|
||||
'distance': totalDistance.toStringAsFixed(2),
|
||||
'status': box.read(BoxName.statusDriverLocation) ?? 'off',
|
||||
};
|
||||
|
||||
// 🔁 كل 5 ثواني - تحديث الموقع
|
||||
// ✅ تحديث للسيرفر
|
||||
await CRUD().post(
|
||||
link:
|
||||
box.read(BoxName.serverChosen) + '/ride/location/update.php',
|
||||
payload: payload,
|
||||
);
|
||||
|
||||
// 📍 كل 60 ثانية - إدخال جديد
|
||||
// ✅ تخزين في SQLite فقط إذا الرحلة On + تحرك أكثر من 10 متر
|
||||
if ((box.read(BoxName.statusDriverLocation) ?? 'off') == 'on') {
|
||||
if (_lastSavedPosition == null ||
|
||||
_calculateDistanceInMeters(_lastSavedPosition!, myLocation) >=
|
||||
10) {
|
||||
await sql.insertData({
|
||||
'driver_id': box.read(BoxName.driverID).toString(),
|
||||
'latitude': myLocation.latitude,
|
||||
'longitude': myLocation.longitude,
|
||||
'created_at': DateTime.now().toIso8601String(),
|
||||
'updated_at': DateTime.now().toIso8601String(),
|
||||
}, 'car_locations');
|
||||
|
||||
_lastSavedPosition = myLocation;
|
||||
}
|
||||
}
|
||||
|
||||
// ✅ إدخال للسيرفر كل دقيقة
|
||||
_insertCounter++;
|
||||
if (_insertCounter >= 12) {
|
||||
_insertCounter = 0;
|
||||
@@ -157,14 +133,14 @@ class LocationController extends GetxController {
|
||||
);
|
||||
}
|
||||
|
||||
// 🔄 تحديث الكاميرا
|
||||
// ✅ تحديث الكاميرا
|
||||
Get.find<HomeCaptainController>()
|
||||
.mapHomeCaptainController
|
||||
?.animateCamera(
|
||||
CameraUpdate.newLatLng(
|
||||
LatLng(
|
||||
Get.find<LocationController>().myLocation.latitude,
|
||||
Get.find<LocationController>().myLocation.longitude,
|
||||
myLocation.latitude,
|
||||
myLocation.longitude,
|
||||
),
|
||||
),
|
||||
);
|
||||
@@ -181,30 +157,18 @@ class LocationController extends GetxController {
|
||||
}
|
||||
|
||||
Future<void> getLocation() async {
|
||||
if (location == null) {
|
||||
location = Location(); // تأكد من أن `Location` مهيأ
|
||||
}
|
||||
|
||||
bool serviceEnabled;
|
||||
PermissionStatus permissionGranted;
|
||||
|
||||
serviceEnabled = await location.serviceEnabled();
|
||||
bool serviceEnabled = await location.serviceEnabled();
|
||||
if (!serviceEnabled) {
|
||||
serviceEnabled = await location.requestService();
|
||||
if (!serviceEnabled) {
|
||||
return;
|
||||
}
|
||||
if (!serviceEnabled) return;
|
||||
}
|
||||
|
||||
permissionGranted = await location.hasPermission();
|
||||
PermissionStatus permissionGranted = await location.hasPermission();
|
||||
if (permissionGranted == PermissionStatus.denied) {
|
||||
permissionGranted = await location.requestPermission();
|
||||
if (permissionGranted != PermissionStatus.granted) {
|
||||
return;
|
||||
}
|
||||
if (permissionGranted != PermissionStatus.granted) return;
|
||||
}
|
||||
|
||||
/// **تجنب استدعاء `getLocation()` مباشرة على الـ Main Thread**
|
||||
Future.delayed(Duration(milliseconds: 500), () async {
|
||||
try {
|
||||
LocationData _locationData = await location.getLocation();
|
||||
@@ -212,80 +176,27 @@ class LocationController extends GetxController {
|
||||
myLocation =
|
||||
LatLng(_locationData.latitude!, _locationData.longitude!);
|
||||
} else {
|
||||
myLocation = LatLng(0, 0); // Default value
|
||||
myLocation = LatLng(0, 0);
|
||||
}
|
||||
|
||||
speed = _locationData.speed ?? 0;
|
||||
heading = _locationData.heading ?? 0;
|
||||
|
||||
if (Get.find<HomeCaptainController>().rideId == 'rideId') {
|
||||
if (previousTime > 0) {
|
||||
double distance = calculateDistanceInKmPerHour(
|
||||
previousTime, _locationData.time, speed);
|
||||
totalDistance += distance;
|
||||
}
|
||||
previousTime = _locationData.time ?? 0;
|
||||
}
|
||||
|
||||
update();
|
||||
} catch (e) {
|
||||
print("Error getting location: $e");
|
||||
}
|
||||
});
|
||||
}
|
||||
// Future<void> 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<HomeCaptainController>().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;
|
||||
double _calculateDistanceInMeters(LatLng start, LatLng end) {
|
||||
const p = 0.017453292519943295;
|
||||
final a = 0.5 -
|
||||
cos((end.latitude - start.latitude) * p) / 2 +
|
||||
cos(start.latitude * p) *
|
||||
cos(end.latitude * p) *
|
||||
(1 - cos((end.longitude - start.longitude) * p)) /
|
||||
2;
|
||||
return 12742 * 1000 * asin(sqrt(a)); // meters
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user