distance static method

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

Calculates the great-circle distance in meters between two points using the Haversine formula.

This formula is fast but can have an error of up to 0.3%. For higher accuracy, use vincentyDistance.

Implementation

static double distance(final ILatLong p1, final ILatLong p2) {
  final sinDLat = sin((degToRadian(p2.latitude) - degToRadian(p1.latitude)) / 2);
  final sinDLng = sin((degToRadian(p2.longitude) - degToRadian(p1.longitude)) / 2);

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

  return EQUATORIAL_RADIUS * c;
}