Files
driver_tripz/lib/controller/notification/ride_available_controller.dart
Hamza-Ayed 0129162309 12/1/1
2024-12-01 10:17:23 +02:00

114 lines
3.3 KiB
Dart

import 'dart:convert';
import 'dart:math';
import 'package:SEFER/constant/box_name.dart';
import 'package:SEFER/controller/functions/location_controller.dart';
import 'package:get/get.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import '../../constant/links.dart';
import '../../main.dart';
import '../../views/widgets/mydialoug.dart';
import '../functions/crud.dart';
class RideAvailableController extends GetxController {
bool isLoading = false;
Map rideAvailableMap = {};
late LatLng southwest;
late LatLng northeast;
// LatLngBounds calculateBounds(
// double centerLat, double centerLng, double radius) {
// // double radius = 4000; // 10 km in meters
// southwest = LatLng(
// centerLat - (radius / 111000),
// centerLng - (radius / (111000 * cos(centerLat))),
// );
// northeast = LatLng(
// centerLat + (radius / 111000),
// centerLng + (radius / (111000 * cos(centerLat))),
// );
// return LatLngBounds(southwest: southwest, northeast: northeast);
// }
LatLngBounds calculateBounds(double lat, double lng, double radiusInMeters) {
const double earthRadius = 6378137.0; // Earth's radius in meters
double latDelta = (radiusInMeters / earthRadius) * (180 / pi);
double lngDelta =
(radiusInMeters / (earthRadius * cos(pi * lat / 180))) * (180 / pi);
double minLat = lat - latDelta;
double maxLat = lat + latDelta;
double minLng = lng - lngDelta;
double maxLng = lng + lngDelta;
// Ensure the latitude is between -90 and 90
minLat = max(-90.0, minLat);
maxLat = min(90.0, maxLat);
// Ensure the longitude is between -180 and 180
minLng = (minLng + 180) % 360 - 180;
maxLng = (maxLng + 180) % 360 - 180;
// Ensure the bounds are in the correct order
if (minLng > maxLng) {
double temp = minLng;
minLng = maxLng;
maxLng = temp;
}
return LatLngBounds(
southwest: LatLng(minLat, minLng),
northeast: LatLng(maxLat, maxLng),
);
}
getRideAvailable() async {
isLoading = true;
LatLngBounds bounds = calculateBounds(
Get.find<LocationController>().myLocation.latitude,
Get.find<LocationController>().myLocation.longitude,
4000);
var res = await CRUD().get(
link: AppLink.getRideWaiting,
payload: {
"carType": box.read(BoxName.carTypeOfDriver).toString(),
'southwestLat': bounds.southwest.latitude.toString(),
'southwestLon': bounds.southwest.longitude.toString(),
'northeastLat': bounds.northeast.latitude.toString(),
'northeastLon': bounds.northeast.longitude.toString(),
},
);
if (res != 'failure') {
rideAvailableMap = jsonDecode(res);
isLoading = false;
update();
} else {
MyDialog().getDialog("No Rides Available".tr, '', () {
Get.back();
Get.back();
});
// Get.defaultDialog(
// title: "No Rides Available".tr,
// middleText: '',
// titleStyle: AppStyle.title,
// confirm: MyElevatedButton(
// title: 'Ok'.tr,
// onPressed: () {
// Get.back();
// Get.back();
// }));
}
}
@override
void onInit() {
getRideAvailable();
super.onInit();
}
}