distance method Null safety

double distance(
  1. GeoPoint p1,
  2. GeoPoint p2
)

Calculates the distance, in kilometers, between two locations, via the Haversine formula. Note that this is approximate due to the fact that the Earth's radius varies between 6356.752 km and 6378.137 km. p1 The first location given p2 The second location given return the distance, in kilometers, between the two locations.

Implementation

static double distance(GeoPoint p1, GeoPoint p2) {
  final dlat = degreesToRadians(p2.latitude - p1.latitude);
  final dlon = degreesToRadians(p2.longitude - p1.longitude);
  final lat1 = degreesToRadians(p1.latitude);
  final lat2 = degreesToRadians(p2.latitude);

  final r = 6378.137; // WGS84 major axis
  double c = 2 *
      asin(sqrt(pow(sin(dlat / 2), 2) +
          cos(lat1) * cos(lat2) * pow(sin(dlon / 2), 2)));
  return r * c;
}