bearingBetween static method

double bearingBetween(
  1. Position startLocation,
  2. Position endLocation
)

Implementation

static double bearingBetween(Position startLocation, Position endLocation) {
  double bearing = 0;
  final double lat1 = startLocation.latitude.toRadians;
  final double lon1 = startLocation.longitude.toRadians;

  final double lat2 = endLocation.latitude.toRadians;
  final double lon2 = endLocation.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 radiansBearing = atan2(y, x);
  bearing = radiansBearing.toDegrees;
  if (bearing < 0) {
    bearing += 360;
  }

  return bearing;
}