calculateHaversineDistance function

double calculateHaversineDistance(
  1. num lat1,
  2. num lon1,
  3. num lat2,
  4. num lon2,
)

Calculates the Haversine distance in meters between two points specified by their latitude and longitude coordinates.

The distance is calculated using the Haversine formula, which provides an approximation of the great-circle distance between two points on a sphere. This function is more suitable when calculating distance with geographic coordinates.

lat1: The latitude of the first point in decimal degrees. lon1: The longitude of the first point in decimal degrees. lat2: The latitude of the second point in decimal degrees. lon2: The longitude of the second point in decimal degrees.

Returns the Haversine distance between the two points in meters.

Implementation

double calculateHaversineDistance(num lat1, num lon1, num lat2, num lon2) {
  var p = 0.017453292519943295; // Pi/180
  var c = cos;
  var a = 0.5 -
      c((lat2 - lat1) * p) / 2 +
      c(lat1 * p) * c(lat2 * p) * (1 - c((lon2 - lon1) * p)) / 2;
  return 12756.274 * asin(sqrt(a)) * 1000.0; // 2*R*asin...
}