haversineDistance static method
Calculates the great-circle distance between two points using the Haversine formula.
Parameters:
lat1: Latitude of point 1 in decimal degreeslon1: Longitude of point 1 in decimal degreeslat2: Latitude of point 2 in decimal degreeslon2: Longitude of point 2 in decimal degrees
Returns: Distance in meters
Example:
final distance = GeoMath.haversineDistance(
37.7749, -122.4194, // San Francisco
40.7128, -74.0060, // New York
);
print(distance); // ~4,130,000 meters (4,130 km)
Accuracy: ~0.5% (assuming spherical Earth)
Implementation
static double haversineDistance(
double lat1,
double lon1,
double lat2,
double lon2,
) {
final dLat = degreesToRadians(lat2 - lat1);
final dLon = degreesToRadians(lon2 - lon1);
final lat1Rad = degreesToRadians(lat1);
final lat2Rad = degreesToRadians(lat2);
final a = pow(sin(dLat / 2), 2) +
cos(lat1Rad) * cos(lat2Rad) * pow(sin(dLon / 2), 2);
final c = 2 * atan2(sqrt(a), sqrt(1 - a));
return earthRadius * c;
}