distance method

  1. @override
double distance(
  1. LatLng p1,
  2. LatLng p2, {
  3. LongitudeDirection lngDir = LongitudeDirection.lazy,
})
override

Calculates distance with Haversine algorithm.

Accuracy can be out by 0.3% More on Wikipedia

Implementation

@override
double distance(final LatLng p1, final LatLng p2,
    {final LongitudeDirection lngDir = LongitudeDirection.lazy}) {
  final dLat = p2.latitudeInRad - p1.latitudeInRad;
  final rawDLng = lngDir.effectiveLongitudinalDelta(p1, p2);

  // If |dLng| > pi we are on the long arc. Haversine only works with the
  // short-arc dLng, so we reduce it and then subtract from 2*pi at the end.
  final isLongArc = rawDLng.abs() > pi;
  final dLng = isLongArc ? rawDLng - (rawDLng > 0 ? tau : -tau) : rawDLng;

  final sinDLat = sin(dLat / 2);
  final sinDLng = sin(dLng / 2);

  // Sides
  final a = sinDLat * sinDLat +
      sinDLng * sinDLng * cos(p1.latitudeInRad) * cos(p2.latitudeInRad);
  final c = 2 * atan2(sqrt(a), sqrt(1 - a));

  return equatorRadius * (isLongArc ? tau - c : c);
}