getClosestCity function Utils

Future<String> getClosestCity(
  1. BuildContext context
)

The getClosestCity function returns the closest city to the user's current location.

Implementation

Future<String> getClosestCity(BuildContext context) async {
  // Get current location
  final currentPosition = await determinePosition(context);
  final cities = await getCities();
  ItalianCities closestCity = cities[0];

  // Calculate distance to each city using Haversine formula
  double minDistance = double.infinity;
  for (final city in cities) {
    final lat1 = currentPosition.latitude;
    final lon1 = currentPosition.longitude;
    final lat2 = city.lat;
    final lon2 = city.lng;

    // Calculate distance
    const R = 6372.8; // In kilometers
    final dLat = toRad(lat2 - lat1);
    final dLon = toRad(lon2 - lon1);
    final a = sin(dLat / 2) * sin(dLat / 2) +
        cos(toRad(lat1)) * cos(toRad(lat2)) * sin(dLon / 2) * sin(dLon / 2);
    final c = 2 * asin(sqrt(a));
    final distance = R * c;

    // Check if distance is the minimum
    if (distance < minDistance) {
      minDistance = distance;
      closestCity = city;
    }
  }

  return closestCity.city;
}