distance function
Calculates the distance, in kilometers, between two locations, via the
Haversine formula. Note that this is approximate due to the fact that
the Earth's radius varies between 6356.752 km and 6378.137 km.
location1
The first location given
location2
The second location given
sreturn the distance, in kilometers, between the two locations.
Implementation
double distance(GeoPoint location1, GeoPoint location2) {
const radius = 6371; // Earth's radius in kilometers
final latDelta = degreesToRadians(location2.latitude - location1.latitude);
final lonDelta = degreesToRadians(location2.longitude - location1.longitude);
final a = (math.sin(latDelta / 2) * math.sin(latDelta / 2)) +
(math.cos(degreesToRadians(location1.latitude)) *
math.cos(degreesToRadians(location2.latitude)) *
math.sin(lonDelta / 2) *
math.sin(lonDelta / 2));
final c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a));
return radius * c;
}