geodeticDistance method

double geodeticDistance({
  1. required double startLatitude,
  2. required double startLongitude,
  3. required double endLatitude,
  4. required double endLongitude,
})

Calculates the distance in meters between two WGS84 points.

Implementation

double geodeticDistance({
  required double startLatitude,
  required double startLongitude,
  required double endLatitude,
  required double endLongitude,
}) {
  // Convert degrees to radians
  final lat1 = startLatitude * math.pi / 180.0;
  final lon1 = startLongitude * math.pi / 180.0;

  final lat2 = endLatitude * math.pi / 180.0;
  final lon2 = endLongitude * math.pi / 180.0;

  // radius of earth in metres
  const r = 6378100;

  // P
  final rho1 = r * math.cos(lat1);
  final z1 = r * math.sin(lat1);
  final x1 = rho1 * math.cos(lon1);
  final y1 = rho1 * math.sin(lon1);

  // Q
  final rho2 = r * math.cos(lat2);
  final z2 = r * math.sin(lat2);
  final x2 = rho2 * math.cos(lon2);
  final y2 = rho2 * math.sin(lon2);

  // Dot product
  final dot = (x1 * x2 + y1 * y2 + z1 * z2);
  final cosTheta = dot / (r * r);
  final theta = math.acos(cosTheta);

  // Distance in meters
  return r * theta;
}