haversineDistance static method

double haversineDistance(
  1. LatLng pos1,
  2. LatLng pos2
)

Implementation

static double haversineDistance(LatLng pos1, LatLng pos2) {
  double lat1 = pos1.latitude * math.pi / 180.0;
  double lon1 = pos1.longitude * math.pi / 180.0;
  double lat2 = pos2.latitude * math.pi / 180.0;
  double lon2 = pos2.longitude * math.pi / 180.0;

  double dLat = lat2 - lat1;
  double dLon = lon2 - lon1;

  double a =
      math.sin(dLat / 2) * math.sin(dLat / 2) +
      math.cos(lat1) *
          math.cos(lat2) *
          math.sin(dLon / 2) *
          math.sin(dLon / 2);
  double c = 2 * math.asin(math.sqrt(a));

  return earthRadiusKm * c * 1000.0; // returns meters
}