import 'dart:math'; // Import for sin, cos, atan2, radians /// Extracts latitude and longitude from a comma-separated string. /// /// The input string is expected to be in the format "latitude,longitude". /// For example: "35.8389801,10.5975386" /// /// Parameters: /// - [locationString]: The string containing the latitude and longitude. /// /// Returns: /// A Map with 'latitude' and 'longitude' as double values, or null /// if the string format is invalid or parsing fails. Map? extractLatLngFromString(String locationString) { final cleanedString = locationString.trim(); final parts = cleanedString.split(','); if (parts.length == 2) { try { final latitude = double.parse(parts[0].trim()); final longitude = double.parse(parts[1].trim()); return {'latitude': latitude, 'longitude': longitude}; } catch (e) { print('Error parsing latitude or longitude from "$locationString": $e'); return null; } } else { print('Invalid location string format: "$locationString". Expected "latitude,longitude".'); return null; } } /// Calculates the distance between a driver's location and a user's location /// using the Haversine formula. /// /// This function takes the driver's current location string and the user's /// start location string (both in "latitude,longitude" format) and /// returns the great-circle distance between them in kilometers. /// /// Returns: /// The distance between the driver and the user in kilometers, or null /// if either location string is invalid. double? calculateDistanceBetweenDriverAndUser( String driverLocationString, String userLocationString, ) { const double earthRadiusKm = 6371; // Earth's mean radius in kilometers // Extract coordinates for the driver final Map? driverCoords = extractLatLngFromString(driverLocationString); if (driverCoords == null) { print("Invalid driver location string provided: $driverLocationString"); return null; } final double driverLatitude = driverCoords['latitude']!; final double driverLongitude = driverCoords['longitude']!; // Extract coordinates for the user final Map? userCoords = extractLatLngFromString(userLocationString); if (userCoords == null) { print("Invalid user location string provided: $userLocationString"); return null; } final double userLatitude = userCoords['latitude']!; final double userLongitude = userCoords['longitude']!; // Convert degrees to radians final double driverLatRad = _toRadians(driverLatitude); final double driverLonRad = _toRadians(driverLongitude); final double userLatRad = _toRadians(userLatitude); final double userLonRad = _toRadians(userLongitude); // Differences in coordinates final double deltaLat = userLatRad - driverLatRad; final double deltaLon = userLonRad - driverLonRad; // Haversine formula components final double a = pow(sin(deltaLat / 2), 2) + cos(driverLatRad) * cos(userLatRad) * pow(sin(deltaLon / 2), 2); final double c = 2 * atan2(sqrt(a), sqrt(1 - a)); // Calculate the distance final double distance = earthRadiusKm * c; return distance; } /// Helper function to convert degrees to radians. double _toRadians(double degree) { return degree * pi / 180; }