25-8-4-1
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'dart:math';
|
||||
import 'package:sefer_driver/controller/home/captin/behavior_controller.dart';
|
||||
import 'package:sefer_driver/controller/home/captin/home_captain_controller.dart';
|
||||
import 'package:sefer_driver/views/widgets/mydialoug.dart';
|
||||
@@ -107,11 +108,16 @@ class MapDriverController extends GetxController {
|
||||
LatLng latLngPassengerLocation = LatLng(0, 0);
|
||||
late LatLng latLngPassengerDestination = LatLng(0, 0);
|
||||
|
||||
List<Map<String, dynamic>> routeSteps = [];
|
||||
String currentInstruction = "";
|
||||
int currentStepIndex = 0;
|
||||
|
||||
void onMapCreated(GoogleMapController controller) async {
|
||||
myLocation = Get.find<LocationController>().myLocation;
|
||||
// myLocation = myLocation;
|
||||
mapController = controller;
|
||||
controller.getVisibleRegion();
|
||||
// LatLngBounds bounds = await controller.getVisibleRegion();
|
||||
controller.animateCamera(
|
||||
CameraUpdate.newLatLng(Get.find<LocationController>().myLocation),
|
||||
);
|
||||
@@ -410,20 +416,20 @@ class MapDriverController extends GetxController {
|
||||
'order_id': (rideId).toString(),
|
||||
'status': 'Begin'
|
||||
});
|
||||
if (AppLink.endPoint != AppLink.seferCairoServer) {
|
||||
CRUD().post(link: "${AppLink.endPoint}/rides/update.php", payload: {
|
||||
'id': (rideId),
|
||||
'rideTimeStart': DateTime.now().toString(),
|
||||
'status': 'Begin',
|
||||
});
|
||||
CRUD().post(
|
||||
link: '${AppLink.endPoint}/rides/driver_order/add.php',
|
||||
payload: {
|
||||
'driver_id': box.read(BoxName.driverID).toString(),
|
||||
'order_id': (rideId).toString(),
|
||||
'status': 'Begin'
|
||||
});
|
||||
}
|
||||
// if (AppLink.endPoint != AppLink.seferCairoServer) {
|
||||
// CRUD().post(link: "${AppLink.endPoint}/rides/update.php", payload: {
|
||||
// 'id': (rideId),
|
||||
// 'rideTimeStart': DateTime.now().toString(),
|
||||
// 'status': 'Begin',
|
||||
// });
|
||||
// CRUD().post(
|
||||
// link: '${AppLink.endPoint}/rides/driver_order/add.php',
|
||||
// payload: {
|
||||
// 'driver_id': box.read(BoxName.driverID).toString(),
|
||||
// 'order_id': (rideId).toString(),
|
||||
// 'status': 'Begin'
|
||||
// });
|
||||
// }
|
||||
Get.find<FirebaseMessagesController>().sendNotificationToDriverMAP(
|
||||
'Trip is Begin'.tr,
|
||||
box.read(BoxName.nameDriver).toString(),
|
||||
@@ -591,28 +597,34 @@ class MapDriverController extends GetxController {
|
||||
}
|
||||
|
||||
Future<void> finishRideFromDriver() async {
|
||||
double distanceToDestination = Geolocator.distanceBetween(
|
||||
latLngPassengerDestination.latitude,
|
||||
latLngPassengerDestination.longitude,
|
||||
Get.find<LocationController>().myLocation.latitude,
|
||||
Get.find<LocationController>().myLocation.longitude,
|
||||
);
|
||||
// double distanceToDestination = Geolocator.distanceBetween(
|
||||
// latLngPassengerDestination.latitude,
|
||||
// latLngPassengerDestination.longitude,
|
||||
// Get.find<LocationController>().myLocation.latitude,
|
||||
// Get.find<LocationController>().myLocation.longitude,
|
||||
// );
|
||||
final originalDistanceM = double.parse(distance.toString()) * 1000;
|
||||
|
||||
// 2. احسب المسافة التي قطعها السائق حتى الآن
|
||||
final movedDistanceM = originalDistanceM - distanceToDestination;
|
||||
final movedDistanceM = Geolocator.distanceBetween(
|
||||
Get.find<LocationController>().myLocation.latitude,
|
||||
Get.find<LocationController>().myLocation.longitude,
|
||||
latLngPassengerDestination.latitude,
|
||||
latLngPassengerDestination.longitude,
|
||||
);
|
||||
// originalDistanceM - distanceToDestination;
|
||||
|
||||
// 3. عتبة ثلث المسافة
|
||||
final oneThirdDistanceM = originalDistanceM / 3;
|
||||
|
||||
// Logging للتتبع
|
||||
Log.print('originalDistanceM: $originalDistanceM');
|
||||
Log.print('distanceToDestinationM: $distanceToDestination');
|
||||
// Log.print('distanceToDestinationM: $distanceToDestination');
|
||||
Log.print('movedDistanceM: $movedDistanceM');
|
||||
Log.print('oneThirdDistanceM: $oneThirdDistanceM');
|
||||
|
||||
// 4. إذا لم يقطع السائق ثلث المسافة، نعرض التأكيد
|
||||
if (movedDistanceM < oneThirdDistanceM) {
|
||||
if (movedDistanceM > oneThirdDistanceM * 2) {
|
||||
MyDialog().getDialog(
|
||||
'Are you sure to exit ride?'.tr,
|
||||
'',
|
||||
@@ -1177,6 +1189,7 @@ class MapDriverController extends GetxController {
|
||||
('${AppLink.googleMapsLink}directions/json?&language=${box.read(BoxName.lang)}&avoid=tolls|ferries&destination=$destination&origin=$origin&key=${AK.mapAPIKEY}');
|
||||
|
||||
var response = await CRUD().getGoogleApi(link: url, payload: {});
|
||||
Log.print('response: ${response}');
|
||||
data = response['routes'][0]['legs'];
|
||||
distanceBetweenDriverAndPassengerWhenConfirm =
|
||||
(data[0]['distance']['value']) / 1000;
|
||||
@@ -1230,6 +1243,84 @@ class MapDriverController extends GetxController {
|
||||
}
|
||||
}
|
||||
|
||||
void checkForNextStep(LatLng currentPosition) {
|
||||
if (currentStepIndex >= routeSteps.length) return;
|
||||
|
||||
final step = routeSteps[currentStepIndex];
|
||||
final endLocation = step['end_location'];
|
||||
final endLatLng = LatLng(endLocation['lat'], endLocation['lng']);
|
||||
|
||||
final distance = calculateDistance(
|
||||
currentPosition.latitude,
|
||||
currentPosition.longitude,
|
||||
endLatLng.latitude,
|
||||
endLatLng.longitude,
|
||||
);
|
||||
|
||||
if (distance < 50) {
|
||||
// 50 متر قبل النقطة
|
||||
currentStepIndex++;
|
||||
if (currentStepIndex < routeSteps.length) {
|
||||
currentInstruction = _parseInstruction(
|
||||
routeSteps[currentStepIndex]['html_instructions']);
|
||||
Get.isRegistered<TextToSpeechController>()
|
||||
? Get.find<TextToSpeechController>().speakText(currentInstruction)
|
||||
: Get.put(TextToSpeechController()).speakText(currentInstruction);
|
||||
Log.print('Current Instruction: $currentInstruction');
|
||||
update();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Calculates the distance in meters between two latitude/longitude points.
|
||||
double calculateDistance(double lat1, double lon1, double lat2, double lon2) {
|
||||
const double earthRadius = 6371000; // meters
|
||||
double dLat = _degreesToRadians(lat2 - lat1);
|
||||
double dLon = _degreesToRadians(lon2 - lon1);
|
||||
|
||||
double a = (sin(dLat / 2) * sin(dLat / 2)) +
|
||||
cos(_degreesToRadians(lat1)) *
|
||||
cos(_degreesToRadians(lat2)) *
|
||||
(sin(dLon / 2) * sin(dLon / 2));
|
||||
double c = 2 * atan2(sqrt(a), sqrt(1 - a));
|
||||
double distance = earthRadius * c;
|
||||
return distance;
|
||||
}
|
||||
|
||||
double _degreesToRadians(double degrees) {
|
||||
return degrees * (3.1415926535897932 / 180.0);
|
||||
}
|
||||
|
||||
String _parseInstruction(String htmlInstruction) {
|
||||
return htmlInstruction.replaceAll(RegExp(r'<[^>]*>'), '');
|
||||
}
|
||||
|
||||
void checkDestinationProximity() {
|
||||
final distance = calculateDistance(
|
||||
myLocation.latitude,
|
||||
myLocation.longitude,
|
||||
latLngPassengerDestination.latitude,
|
||||
latLngPassengerDestination.longitude,
|
||||
);
|
||||
|
||||
if (distance < 300) {
|
||||
// 300 متر قبل الوجهة
|
||||
Get.find<FirebaseMessagesController>().sendNotificationToDriverMAP(
|
||||
"You are near the destination".tr,
|
||||
"You are near the destination".tr,
|
||||
tokenPassenger,
|
||||
[
|
||||
box.read(BoxName.driverID),
|
||||
rideId,
|
||||
box.read(BoxName.tokenDriver),
|
||||
paymentAmount.toString()
|
||||
],
|
||||
'ding.wav',
|
||||
);
|
||||
// يمكن إضافة أي إجراء آخر هنا عند الاقتراب من الوجهة
|
||||
}
|
||||
}
|
||||
|
||||
getMapDestination(String origin, destination) async {
|
||||
var url =
|
||||
('${AppLink.googleMapsLink}directions/json?&language=${box.read(BoxName.lang)}&avoid=tolls|ferries&destination=$destination&origin=$origin&key=${AK.mapAPIKEY}');
|
||||
@@ -1243,6 +1334,22 @@ class MapDriverController extends GetxController {
|
||||
double lng = points[i][1].toDouble();
|
||||
polylineCoordinatesDestination.add(LatLng(lat, lng));
|
||||
}
|
||||
// استخراج الخطوات
|
||||
routeSteps = List<Map<String, dynamic>>.from(dataDestination[0]['steps']);
|
||||
Log.print('routeSteps: ${routeSteps}');
|
||||
currentStepIndex = 0;
|
||||
if (routeSteps.isNotEmpty) {
|
||||
currentInstruction =
|
||||
_parseInstruction(routeSteps[0]['html_instructions']);
|
||||
Log.print('currentInstruction: ${currentInstruction}');
|
||||
Get.isRegistered<TextToSpeechController>()
|
||||
? Get.find<TextToSpeechController>().speakText(currentInstruction)
|
||||
: Get.put(TextToSpeechController()).speakText(currentInstruction);
|
||||
}
|
||||
update();
|
||||
|
||||
// دالة مساعدة لتنظيف التعليمات
|
||||
|
||||
if (polyLinesDestination.isNotEmpty) {
|
||||
// clearPolyline();
|
||||
var polyline = Polyline(
|
||||
@@ -1404,6 +1511,7 @@ class MapDriverController extends GetxController {
|
||||
hours = durationToAdd.inHours;
|
||||
minutes = (durationToAdd.inMinutes % 60).round();
|
||||
calculateConsumptionFuel();
|
||||
updateLocation();
|
||||
// cancelCheckRidefromPassenger();
|
||||
// checkIsDriverNearPassenger();
|
||||
super.onInit();
|
||||
|
||||
Reference in New Issue
Block a user