azimuth method

double azimuth(
  1. Coordinates target
)

Calculates the azimuth (bearing) from this coordinate to target.

The azimuth is the initial great-circle bearing on a spherical earth, clockwise from north. If the points are identical or either is invalid, the method returns 0.0.

Parameters

  • target: The destination Coordinates to which the azimuth is calculated.

Returns

  • Azimuth in degrees from this coordinate to target, in [0, 360).

Implementation

double azimuth(Coordinates target) {
  if (!isValid || !target.isValid) {
    return 0.0;
  }

  final double lat1 = latitude * pi / 180;
  final double lat2 = target.latitude * pi / 180;
  final double dLon = (target.longitude - longitude) * pi / 180;
  final double y = sin(dLon) * cos(lat2);
  final double x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon);

  final double azimuth = (atan2(y, x) * 180 / pi) % 360;

  // A tiny negative angle (target at the pole, west of the start) wraps to exactly 360.
  return azimuth < 360 ? azimuth : 0.0;
}