Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static const double earthRadius = 6371000; // Earth's radius in meters
- static double haversine(double lat1, double lon1, double lat2, double lon2) {
- // Convert degrees to radians
- double radLat1 = _toRadians(lat1);
- double radLat2 = _toRadians(lat2);
- double radLon1 = _toRadians(lon1);
- double radLon2 = _toRadians(lon2);
- // Differences in coordinates
- double dLat = radLat2 - radLat1;
- double dLon = radLon2 - radLon1;
- // Haversine formula
- double a = pow(sin(dLat / 2), 2) +
- cos(radLat1) * cos(radLat2) * pow(sin(dLon / 2), 2);
- double c = 2 * atan2(sqrt(a), sqrt(1 - a));
- return earthRadius * c; // Distance in meters
- }
- static double _toRadians(double degree) {
- return degree * pi / 180;
- }
- static Future<void> _startRideDistancePolling(BuildContext context, String bikeId) async {
- double previousLat; // Previous latitude
- double previousLon; // Previous longitude
- double totalDistance = 0; // Total distance traveled
- Timer? _timer; // Timer for periodic fetching
- // Fetch previous bike data
- var results2 = await BikeController.getSingleBikeData(bikeId);
- previousLat = double.parse(results2['data'][0]['current_latitude']);
- previousLon = double.parse(results2['data'][0]['current_longitude']);
- Timer.periodic(const Duration(seconds: 5), (timer) async {
- var results = await BikeController.fetchSingleBike(bikeId);
- if(results['status'] == 0) { // Failed
- _showSnackbar(context, "Status: ${results['status']}, Message: ${results['message']}");
- }
- _updateRideMarkerRealTime(results['data']);
- //SharedState.currentRideDistance.value = results['data']['']; // TODO: Assign total distance here
- var results1 = await BikeController.getSingleBikeData(bikeId);
- double lat = double.parse(results1['data'][0]['current_latitude']);
- double long = double.parse(results1['data'][0]['current_longitude']);
- if(previousLat!=lat||previousLon!=long){
- double distance = haversine(previousLat, previousLon, lat, long);
- int new_distance = distance.toInt();
- totalDistance += new_distance;
- //SharedState.currentRideDistance.value = distance.toString();
- Fluttertoast.showToast(
- msg: "First Latitude ="+previousLat.toString()+ " Second Latitude = "+lat.toString()+ "\n Distance = "+distance.toString()+"\n Total Distance = " + totalDistance.toString(),
- toastLength: Toast.LENGTH_SHORT,
- gravity: ToastGravity.BOTTOM,
- backgroundColor: Colors.blue,
- textColor: Colors.white,
- fontSize: 16.0,
- );
- previousLat = lat; // Update previous latitude to current latitude
- previousLon = long; // Update previous latitude to current latitude
- }
- // Stop the timer once ride session ends (to conserve ram)
- if(SharedState.isRiding.value == false) {
- SharedState.visibleMarkers.value.removeWhere((marker) => marker.key == const ValueKey("riding_marker"));
- timer.cancel();
- int last_distance = totalDistance.toInt();
- SharedState.currentRideDistance.value = totalDistance.toString();
- String bike_id = "B25001";
- //SharedState.bikeId.value = bike_id;
- //last_distance = 10;
- SharedState.currentRideDistance.value = last_distance.toString();
- print("Last Distance " + last_distance.toString());
- await RideController.updateRideDistance(bikeId, last_distance);
- Fluttertoast.showToast(
- msg: "Your Total Distance = "+SharedState.currentRideDistance.value,
- toastLength: Toast.LENGTH_SHORT,
- gravity: ToastGravity.BOTTOM,
- backgroundColor: Colors.blue,
- textColor: Colors.white,
- fontSize: 16.0,
- );
- };
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement