distanceTo method

double distanceTo(
  1. Coordinates other
)

Calculates the distance in meters between two coordinates using the Haversine formula

Implementation

double distanceTo(Coordinates other) {
  const double earthRadius = 6371000; // Earth radius in meters
  final double lat1Rad = latitude * pi / 180;
  final double lat2Rad = other.latitude * pi / 180;
  final double deltaLatRad = (other.latitude - latitude) * pi / 180;
  final double deltaLonRad = (other.longitude - longitude) * pi / 180;

  final double a = sin(deltaLatRad / 2) * sin(deltaLatRad / 2) +
      cos(lat1Rad) *
          cos(lat2Rad) *
          sin(deltaLonRad / 2) *
          sin(deltaLonRad / 2);
  final double c = 2 * atan2(sqrt(a), sqrt(1 - a));

  return earthRadius * c;
}