initialBearing static method

double initialBearing(
  1. LatLng start,
  2. LatLng end
)

Calculates the initial bearing (azimuth) from one point to another.

start - The LatLng coordinates of the starting point.

end - The LatLng coordinates of the ending point.

Returns the initial bearing in degrees, where 0 degrees indicates a northward direction and 90 degrees indicates an eastward direction.

Implementation

static double initialBearing(LatLng start, LatLng end) {
  final double lat1 = (start.latitude).toRadians();
  final double lon1 = (start.longitude).toRadians();
  final double lat2 = (end.latitude).toRadians();
  final double lon2 = (end.longitude).toRadians();

  final double dLon = lon2 - lon1;

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

  // Convert initial bearing from radians to degrees
  return (initialBearings).toDegrees();
}