alongTrackDistanceTo method

double alongTrackDistanceTo({
  1. required Coordinate currentPoint,
  2. required Coordinate startPoint,
  3. required Coordinate endPoint,
})

This function returns how far currentPoint is along a path from from startPoint, heading towards endPoint. That is, if a perpendicular is drawn from currentPoint point to the (great circle) path, the along-track distance is the distance from the start point to where the perpendicular crosses the path.

currentPoint The point whose distance is wondering to great circle startPoint Start of the great circle endPoint End of the great circle Returns Distance along great circle to point nearest currentPoint point.

final istCoordinates = Coordinate(41.28111111, 28.75333333); // The coordinates of Istanbul Airport
final jfkCoordinates = Coordinate(40.63980103, -73.77890015); // The coordinates of New York JFK Airport
final fcoCoordinates = Coordinate(41.8002778,12.2388889); // The coordinates of Roma Fiumicino Airport
final alongTrackDistanceTo = greatCircle.alongTrackDistanceTo(fcoCoordinates, istCoordinates, jfkCoordinates);

Implementation

double alongTrackDistanceTo(
    {required Coordinate currentPoint,
    required Coordinate startPoint,
    required Coordinate endPoint}) {
  if (currentPoint == startPoint) return 0;

  final double delta13 = distance(startPoint, currentPoint) / earthRadiusKm;
  final double teta13 = bearing(startPoint, currentPoint).toRadians();
  final double teta12 = bearing(startPoint, endPoint).toRadians();

  final double deltaCrossTrack = asin(sin(delta13) * sin(teta12 - teta13));
  final double deltaAlongTrack =
      acos(cos(delta13) / cos(deltaCrossTrack).abs());

  return deltaAlongTrack * cos(teta12 - teta13).sign * earthRadiusKm;
}