calculateBearing static method

double calculateBearing(
  1. double lat1,
  2. double lon1,
  3. double lat2,
  4. double lon2,
)

Calculates the initial bearing (forward azimuth) from point 1 to point 2.

The bearing is the angle measured clockwise from true north.

Parameters:

  • lat1: Latitude of starting point in decimal degrees
  • lon1: Longitude of starting point in decimal degrees
  • lat2: Latitude of destination point in decimal degrees
  • lon2: Longitude of destination point in decimal degrees

Returns: Initial bearing in degrees (0-360)

Example:

final bearing = GeoMath.calculateBearing(
  37.7749, -122.4194, // San Francisco
  40.7128, -74.0060,  // New York
);
print(bearing); // ~68 degrees (east-northeast)

Implementation

static double calculateBearing(
  double lat1,
  double lon1,
  double lat2,
  double lon2,
) {
  final lat1Rad = degreesToRadians(lat1);
  final lat2Rad = degreesToRadians(lat2);
  final dLon = degreesToRadians(lon2 - lon1);

  final y = sin(dLon) * cos(lat2Rad);
  final x = cos(lat1Rad) * sin(lat2Rad) -
      sin(lat1Rad) * cos(lat2Rad) * cos(dLon);

  final bearing = radiansToDegrees(atan2(y, x));

  return normalizeDegrees(bearing);
}