Calculate average latitude and longitude in C #

I have (minLatitude, maxLatitude) and (minLongitude, maxLongitude) pairs in decimal degrees, and I need to calculate the midpoint of the field that these pairs define, how can I get their average latitude / longitude? I get a fairly large bias when calculating the arithmetic mean.

Thanks!

+4
source share
2 answers

The solution proposed by Pierre-Luc Champigny is incorrect.

In the following figure, you can see two lines:

  • (lat1, lon1) → (lat2, lon2)
  • (lat1, lon2) → (lat2, lon1)

Half the length of each line is green, the other half is blue.

You can see that the center of both lines is not the same point, and both centers are not the centers of the polygon.

earth

To find the center of a polygon:

  • lat = avrg (lat1, lat2)
  • lon = avrg (lon1, lon2)

To get these values, you can use the link suggested by Pierre-Luc Champigny, but:

  • Take lat midpoint (lat1, lon1) -> (lat2, lon1)
  • Take lon mid (lat1, lon1) -> (lat1, lon2)
+7
source

From: http://www.movable-type.co.uk/scripts/latlong.html

Your values ​​should be all in radians, so use 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); 
+5
source

All Articles