bearingTo method

double bearingTo(
  1. Coordinate other, {
  2. AngleUnit? unit,
})

Returns the bearing to a different Coordinate in degrees, or specified unit. Uses the Haversine formula to calculate the bearing between two points.

Example:

Coordinate(1, 2).bearingTo(Coordinate(3, 4)); // 45.0

Implementation

double bearingTo(Coordinate other, {AngleUnit? unit}) {
  final lat1 = latitude * (pi / 180);
  final lon1 = longitude * (pi / 180);
  final lat2 = other.latitude * (pi / 180);
  final lon2 = other.longitude * (pi / 180);

  final dLon = lon2 - lon1;

  final y = sin(dLon) * cos(lat2);
  final x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon);
  final bearing = atan2(y, x);

  return convertAngle((bearing * (180 / pi) + 360) % 360, AngleUnits.degrees,
      unit ?? AngleUnits.degrees);
}