distance method

double distance(
  1. Coordinates other
)

Calculate the distance in meters between two WGS84 coordinates.

Parameters

  • IN other The other coordinates

Returns

Distance in meters between current current coordinates and the other parameter

Implementation

double distance(final Coordinates other) {
  const double earthRadius = 6371000; // Earth's radius in meters

  double toRadians(final double value) {
    return value * pi / 180; // Convert degrees to radians
  }

  final double deltaLatitude = toRadians(other.latitude - latitude);
  final double deltaLongitude = toRadians(other.longitude - longitude);

  final double a = sin(deltaLatitude / 2) * sin(deltaLatitude / 2) +
      cos(toRadians(latitude)) *
          cos(toRadians(other.latitude)) *
          sin(deltaLongitude / 2) *
          sin(deltaLongitude / 2);

  final double c = 2 * atan2(sqrt(a), sqrt(1 - a));

  double altitudeDifference = 0;
  if (other.altitude != null && altitude != null) {
    altitudeDifference = other.altitude! - altitude!;
  }

  final double distance = sqrt(
    (c * c * earthRadius * earthRadius) +
        (altitudeDifference * altitudeDifference),
  );

  return distance;
}