Advertisement
Zuhairy_Harry

Ride Distance Calculation Zu

Jan 21st, 2025
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 4.02 KB | None | 0 0
  1.   static const double earthRadius = 6371000; // Earth's radius in meters
  2.  
  3.   static double haversine(double lat1, double lon1, double lat2, double lon2) {
  4.     // Convert degrees to radians
  5.     double radLat1 = _toRadians(lat1);
  6.     double radLat2 = _toRadians(lat2);
  7.     double radLon1 = _toRadians(lon1);
  8.     double radLon2 = _toRadians(lon2);
  9.  
  10.     // Differences in coordinates
  11.     double dLat = radLat2 - radLat1;
  12.     double dLon = radLon2 - radLon1;
  13.  
  14.     // Haversine formula
  15.     double a = pow(sin(dLat / 2), 2) +
  16.         cos(radLat1) * cos(radLat2) * pow(sin(dLon / 2), 2);
  17.     double c = 2 * atan2(sqrt(a), sqrt(1 - a));
  18.  
  19.     return earthRadius * c; // Distance in meters
  20.   }
  21.  
  22.   static double _toRadians(double degree) {
  23.     return degree * pi / 180;
  24.   }
  25.  
  26.   static Future<void> _startRideDistancePolling(BuildContext context, String bikeId) async {
  27.     double previousLat; // Previous latitude
  28.     double previousLon; // Previous longitude
  29.     double totalDistance = 0; // Total distance traveled
  30.     Timer? _timer; // Timer for periodic fetching
  31.  
  32.     // Fetch previous bike data
  33.     var results2 = await BikeController.getSingleBikeData(bikeId);
  34.     previousLat = double.parse(results2['data'][0]['current_latitude']);
  35.     previousLon = double.parse(results2['data'][0]['current_longitude']);
  36.  
  37.     Timer.periodic(const Duration(seconds: 5), (timer) async {
  38.       var results = await BikeController.fetchSingleBike(bikeId);
  39.       if(results['status'] == 0) { // Failed
  40.         _showSnackbar(context, "Status: ${results['status']}, Message: ${results['message']}");
  41.       }
  42.       _updateRideMarkerRealTime(results['data']);
  43.       //SharedState.currentRideDistance.value = results['data']['']; // TODO: Assign total distance here
  44.  
  45.  
  46.  
  47.       var results1 = await BikeController.getSingleBikeData(bikeId);
  48.       double lat = double.parse(results1['data'][0]['current_latitude']);
  49.       double long = double.parse(results1['data'][0]['current_longitude']);
  50.  
  51.       if(previousLat!=lat||previousLon!=long){
  52.         double distance = haversine(previousLat, previousLon, lat, long);
  53.           int new_distance = distance.toInt();
  54.           totalDistance += new_distance;
  55.        
  56.        
  57.       //SharedState.currentRideDistance.value = distance.toString();
  58.       Fluttertoast.showToast(
  59.                 msg: "First Latitude ="+previousLat.toString()+ " Second Latitude = "+lat.toString()+ "\n Distance = "+distance.toString()+"\n Total Distance = " + totalDistance.toString(),
  60.                 toastLength: Toast.LENGTH_SHORT,
  61.                 gravity: ToastGravity.BOTTOM,
  62.                 backgroundColor: Colors.blue,
  63.                 textColor: Colors.white,
  64.                 fontSize: 16.0,
  65.               );
  66.  
  67.         previousLat = lat; // Update previous latitude to current latitude
  68.         previousLon = long; // Update previous latitude to current latitude
  69.  
  70.       }
  71.  
  72.      
  73.  
  74.  
  75.       // Stop the timer once ride session ends (to conserve ram)
  76.       if(SharedState.isRiding.value == false) {
  77.         SharedState.visibleMarkers.value.removeWhere((marker) => marker.key == const ValueKey("riding_marker"));
  78.         timer.cancel();
  79.        
  80.  
  81.               int last_distance = totalDistance.toInt();
  82.               SharedState.currentRideDistance.value = totalDistance.toString();
  83.  
  84.               String bike_id = "B25001";
  85.               //SharedState.bikeId.value = bike_id;
  86.               //last_distance = 10;
  87.               SharedState.currentRideDistance.value = last_distance.toString();
  88.               print("Last Distance " + last_distance.toString());
  89.               await RideController.updateRideDistance(bikeId, last_distance);
  90.  
  91.  
  92.  
  93.               Fluttertoast.showToast(
  94.                 msg: "Your Total Distance = "+SharedState.currentRideDistance.value,
  95.                 toastLength: Toast.LENGTH_SHORT,
  96.                 gravity: ToastGravity.BOTTOM,
  97.                 backgroundColor: Colors.blue,
  98.                 textColor: Colors.white,
  99.                 fontSize: 16.0,
  100.               );
  101.  
  102.  
  103.  
  104.       };
  105.     });
  106.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement