calculateDistance static method

double calculateDistance(
  1. dynamic lat1,
  2. dynamic lon1,
  3. dynamic lat2,
  4. dynamic lon2,
)

Calculates the distance in miles between two geographic coordinates.

Uses the Haversine formula to compute the great-circle distance between two points on Earth's surface. The result is returned in miles.

Parameters:

  • lat1: Latitude of the first point (degrees, -90 to 90)
  • lon1: Longitude of the first point (degrees, -180 to 180)
  • lat2: Latitude of the second point (degrees, -90 to 90)
  • lon2: Longitude of the second point (degrees, -180 to 180)

Returns the distance in miles as a double.

Example:

// Distance from New York to Los Angeles
final distance = RtCommonFunction.calculateDistance(
  40.7128,  // NYC latitude
  -74.0060, // NYC longitude
  34.0522,  // LA latitude
  -118.2437 // LA longitude
);
print('Distance: ${distance.toStringAsFixed(2)} miles');

Note: The formula assumes Earth is a perfect sphere with radius of 6371 km. The result is converted from kilometers to miles (1 km ≈ 0.621371 miles).

Implementation

static double calculateDistance(
    dynamic lat1, dynamic lon1, dynamic lat2, dynamic lon2) {
  var p = 0.017453292519943295;
  var c = cos;
  var a = 0.5 -
      c((lat2 - lat1) * p) / 2 +
      c(lat1 * p) * c(lat2 * p) * (1 - c((lon2 - lon1) * p)) / 2;
  //print("Distance from Current  Lat & lag"+(12742 * asin(sqrt(a))*0.621371).toString());
  return 12742 * asin(sqrt(a)) * 0.621371;
}