Reputation: 75
I have (minLatitude,maxLatitude) and (minLongitude,maxLongitude) pairs in decimal degrees, and I need to calculate the mean point of the box those pairs define, how can I get their average latitude/longitude? I get a pretty big offset when the simple arithmetic mean is calculated.
Thanks!
Upvotes: 4
Views: 3003
Reputation: 20618
The solution suggested by Pierre-Luc Champigny is wrong.
In the image bellow you can see two lines:
Half length of each line in green, the other half in blue.
You can see that the center of both lines is not the same point, and both centers are not the center of the polygon.
In order to find the center of the polygon:
To get these values you can use the link suggested by Pierre-Luc Champigny, but:
Upvotes: 8
Reputation: 1851
From: http://www.movable-type.co.uk/scripts/latlong.html
Your values should be all in radian, so using the midpoint formula:
var lat1 = minLatitude; // In radian
var lat2 = maxLatitude; // In radian
var lon1 = minLongitude; // In radian
var lon2 = maxLongitude; // In radian
var dLon = (lon2-lon1);
var Bx = Math.Cos(lat2) * Math.Cos(dLon);
var By = Math.Cos(lat2) * Math.Sin(dLon);
var avgLat = Math.Atan2(
Math.Sin(lat1) +
Math.Sin(lat2),
Math.Sqrt((
Math.Cos(lat1)+Bx) * (Math.Cos(lat1)+Bx) + By*By));
var avgLong = lon1 + Math.Atan2(By, Math.Cos(lat1) + Bx);
Upvotes: 5