bearingTo method Null safety

double bearingTo(
  1. Coordinate other
)

Returns the bearing to a different Coordinate in degrees. 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) {
  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 (bearing * (180 / pi) + 360) % 360;
}