distanceBetween static method

double distanceBetween(
  1. double lat1,
  2. double lng1,
  3. double lat2,
  4. double lng2,
)

Returns the distance between 2 points of coordinates with Haversine formula

@see https://en.wikipedia.org/wiki/Haversine_formula @see https://stackoverflow.com/a/1502821/4241030 @param lat1 Latitude of the point A @param lng1 Longitude of the point A @param lat2 Latitude of the point B @param lng2 Longitude of the point B

Implementation

static double distanceBetween(
    double lat1, double lng1, double lat2, double lng2) {
  // The radius of the planet earth in meters
  const R = 6378137;
  final dLat = _degreesToRadians(lat2 - lat1);
  final dLong = _degreesToRadians(lng2 - lng1);
  final a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
      Math.cos(_degreesToRadians(lat1)) *
          Math.cos(_degreesToRadians(lat1)) *
          Math.sin(dLong / 2) *
          Math.sin(dLong / 2);

  final c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  final distance = R * c;

  return distance;
}