calculateMidpoint static method
Calculates the midpoint between two points on the Earth's surface.
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: A map containing 'latitude' and 'longitude' of the midpoint
Example:
final midpoint = GeoMath.calculateMidpoint(
37.7749, -122.4194, // San Francisco
40.7128, -74.0060, // New York
);
Implementation
static Map<String, double> calculateMidpoint(
double lat1,
double lon1,
double lat2,
double lon2,
) {
final lat1Rad = degreesToRadians(lat1);
final lon1Rad = degreesToRadians(lon1);
final lat2Rad = degreesToRadians(lat2);
final dLon = degreesToRadians(lon2 - lon1);
final bx = cos(lat2Rad) * cos(dLon);
final by = cos(lat2Rad) * sin(dLon);
final latMidRad = atan2(
sin(lat1Rad) + sin(lat2Rad),
sqrt((cos(lat1Rad) + bx) * (cos(lat1Rad) + bx) + by * by),
);
final lonMidRad = lon1Rad + atan2(by, cos(lat1Rad) + bx);
return {
'latitude': radiansToDegrees(latMidRad),
'longitude': radiansToDegrees(lonMidRad),
};
}