2026-04-05-add navigation page and correct whatsapp link
This commit is contained in:
@@ -2995,7 +2995,6 @@ class MapPassengerController extends GetxController {
|
||||
Future<Map<String, double>?> extractCoordinatesFromLinkAsync(
|
||||
String link) async {
|
||||
try {
|
||||
// 1. استخراج الرابط فقط من النص (في حال كان هناك نص مع الرابط في الواتساب)
|
||||
int urlStartIndex = link.indexOf(RegExp(r'https?://'));
|
||||
if (urlStartIndex == -1) return null;
|
||||
String cleanLink = link.substring(urlStartIndex).trim();
|
||||
@@ -3003,67 +3002,44 @@ class MapPassengerController extends GetxController {
|
||||
Uri uri = Uri.parse(cleanLink);
|
||||
String finalUrl = cleanLink;
|
||||
|
||||
// 2. فك الروابط المختصرة (Unshorten URLs)
|
||||
// فك التوجيه للروابط المختصرة
|
||||
if (cleanLink.contains('goo.gl') ||
|
||||
cleanLink.contains('maps.app.goo.gl')) {
|
||||
cleanLink.contains('maps.google.com')) {
|
||||
try {
|
||||
// نقوم بطلب HTTP عادي، وhttp يتبع التوجيه التلقائي (Redirects)
|
||||
var response = await http.get(uri);
|
||||
// نأخذ الرابط النهائي بعد التوجيه
|
||||
var response =
|
||||
await http.get(uri).timeout(const Duration(seconds: 5));
|
||||
finalUrl = response.request?.url.toString() ?? cleanLink;
|
||||
} catch (e) {
|
||||
Log.print('Failed to follow redirect: $e');
|
||||
Log.print('Redirect logic failed, using original: $e');
|
||||
}
|
||||
}
|
||||
|
||||
Log.print('Final Unshortened URL: $finalUrl');
|
||||
// الأنماط المشتركة لخرائط جوجل (تكون دائماً Lat ثم Lng)
|
||||
RegExp regex = RegExp(r'(-?\d+\.\d+)[,/~](-?\d+\.\d+)');
|
||||
var match = regex.firstMatch(finalUrl);
|
||||
|
||||
// 3. استخراج الإحداثيات باستخدام تعبيرات نمطية (Regex) قوية تغطي خرائط جوجل وغيرها
|
||||
if (match != null) {
|
||||
double lat = double.parse(match.group(1)!);
|
||||
double lng = double.parse(match.group(2)!);
|
||||
|
||||
// 🔥 منطق التصحيح الذاتي (Smart Swap) للمنطقة (سوريا/الأردن/مصر)
|
||||
// إذا كان الرقم الأول أكبر من الرقم الثاني بشكل واضح، فهذا يعني أن الرابط مقلوب أو أننا نحتاج للتأكد
|
||||
// في منطقتنا Latitude حوالي 30-35 و Longitude حوالي 36-44
|
||||
if (lat > 40 && lat > lng) {
|
||||
Log.print("⚠️ Detected Swapped Coordinates in Link. Correcting...");
|
||||
double temp = lat;
|
||||
lat = lng;
|
||||
lng = temp;
|
||||
}
|
||||
|
||||
// النمط الأول: @lat,lng (الأكثر شيوعاً في خرائط جوجل)
|
||||
RegExp regexAt = RegExp(r'@(-?\d+\.\d+),(-?\d+\.\d+)');
|
||||
var matchAt = regexAt.firstMatch(finalUrl);
|
||||
if (matchAt != null) {
|
||||
return {
|
||||
'latitude': double.parse(matchAt.group(1)!),
|
||||
'longitude': double.parse(matchAt.group(2)!),
|
||||
};
|
||||
}
|
||||
|
||||
// النمط الثاني: q=lat,lng أو ll=lat,lng أو query=lat,lng
|
||||
RegExp regexQuery =
|
||||
RegExp(r'(?:q|ll|query)=(-?\d+\.\d+)[,~](-?\d+\.\d+)');
|
||||
var matchQuery = regexQuery.firstMatch(finalUrl);
|
||||
if (matchQuery != null) {
|
||||
return {
|
||||
'latitude': double.parse(matchQuery.group(1)!),
|
||||
'longitude': double.parse(matchQuery.group(2)!),
|
||||
};
|
||||
}
|
||||
|
||||
// النمط الثالث: search/lat,lng (موجود في بعض أشكال خرائط جوجل)
|
||||
RegExp regexSearch = RegExp(r'search/(-?\d+\.\d+),(-?\d+\.\d+)');
|
||||
var matchSearch = regexSearch.firstMatch(finalUrl);
|
||||
if (matchSearch != null) {
|
||||
return {
|
||||
'latitude': double.parse(matchSearch.group(1)!),
|
||||
'longitude': double.parse(matchSearch.group(2)!),
|
||||
};
|
||||
}
|
||||
|
||||
// النمط الرابع: place/lat,lng (غالباً متواجد في الروابط المشتركة من خرائط جوجل)
|
||||
RegExp regexPlace = RegExp(r'place/(-?\d+\.\d+),(-?\d+\.\d+)');
|
||||
var matchPlace = regexPlace.firstMatch(finalUrl);
|
||||
if (matchPlace != null) {
|
||||
return {
|
||||
'latitude': double.parse(matchPlace.group(1)!),
|
||||
'longitude': double.parse(matchPlace.group(2)!),
|
||||
'latitude': lat,
|
||||
'longitude': lng,
|
||||
};
|
||||
}
|
||||
} catch (e) {
|
||||
Log.print('Error parsing location link: $e');
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -3087,16 +3063,36 @@ class MapPassengerController extends GetxController {
|
||||
|
||||
void goToWhatappLocation() async {
|
||||
if (sosFormKey.currentState!.validate()) {
|
||||
changeIsWhatsAppOrder(true);
|
||||
Get.back();
|
||||
handleWhatsAppLink(whatsAppLocationText.text);
|
||||
myDestination = LatLng(latitudeWhatsApp, longitudeWhatsApp);
|
||||
await mapController?.animateCamera(CameraUpdate.newLatLng(
|
||||
LatLng(passengerLocation.latitude, passengerLocation.longitude)));
|
||||
changeMainBottomMenuMap();
|
||||
passengerStartLocationFromMap = true;
|
||||
isPickerShown = true;
|
||||
update();
|
||||
// 1. استخراج الإحداثيات أولاً بشكل محلي لضمان عدم حدوث سباق بيانات (Race Condition)
|
||||
Map<String, double>? coordinates =
|
||||
await extractCoordinatesFromLinkAsync(whatsAppLocationText.text);
|
||||
|
||||
if (coordinates != null) {
|
||||
latitudeWhatsApp = coordinates['latitude']!;
|
||||
longitudeWhatsApp = coordinates['longitude']!;
|
||||
|
||||
Log.print(
|
||||
'📍 Final Coordinates for OSM: Lat: $latitudeWhatsApp, Lng: $longitudeWhatsApp');
|
||||
|
||||
changeIsWhatsAppOrder(true);
|
||||
Get.back();
|
||||
|
||||
// إعداد الوجهة
|
||||
myDestination = LatLng(latitudeWhatsApp, longitudeWhatsApp);
|
||||
|
||||
// تحريك الكاميرا لموقع الراكب (البداية) وليس الوجهة فوراً لضمان تحميل الخريطة
|
||||
if (passengerLocation != null) {
|
||||
await mapController?.animateCamera(CameraUpdate.newLatLng(
|
||||
LatLng(passengerLocation.latitude, passengerLocation.longitude)));
|
||||
}
|
||||
|
||||
changeMainBottomMenuMap();
|
||||
passengerStartLocationFromMap = true;
|
||||
isPickerShown = true;
|
||||
update();
|
||||
} else {
|
||||
mySnackbarWarning('لم نتمكن من استخراج الموقع من الرابط');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user