crossTrackDistanceTo method

double crossTrackDistanceTo({
  1. required Geographic start,
  2. required Geographic end,
  3. double radius = 6371000.0,
})

Returns (signed) distance from the current position to great circle defined by start and end points.

Parameters:

  • start: The start point on a great circle path.
  • end: The end point on a great circle path.
  • radius: The radius of earth (defaults to mean radius in metres).

The returned value is a distance to a great circle (-ve if to left, +ve if to right of path).

Examples:

  const pCurrent = Geographic(lat: 53.2611, lon: -0.7972);
  const p1 =  Geographic(lat: 53.3206, lon: -1.7297);
  const p2 =  Geographic(lat: 53.1887, lon: 0.1334);

  // cross track distance: -307.5 m
  final d = pCurrent.spherical.crossTrackDistanceTo(start: p1, end: p2);

Implementation

double crossTrackDistanceTo({
  required Geographic start,
  required Geographic end,
  double radius = 6371000.0,
}) {
  if (position == start) return 0;

  final spherical = start.spherical;
  final dst13 = spherical.distanceTo(position, radius: radius) / radius;
  final brng13 = spherical.initialBearingTo(position).toRadians();
  final brng12 = spherical.initialBearingTo(end).toRadians();

  final dstxt = asin(sin(dst13) * sin(brng13 - brng12));

  return dstxt * radius;
}