# How to calculate center point in geographic coordinates?

How can I calculate center point of $N$ latitude/longitude pairs, if the distance is at the most $30m$? Assuming altitude is same for all points.

• First you need to define what you mean by "center". There are many different such definitions in use, especially ones invented by cities that want to have the center of such-and-such country or continent X inside their territory. Commented Dec 12, 2012 at 0:22
• I will try. I have to get single coordinate from GPS. Unfortunately it does not have built in averaging function. So it gives me coordinates with certain error. I can however collect dozens of coordinates for that exact position. But i don't know how to average these coordinates into single which can more or less smoothen gps error. Commented Dec 12, 2012 at 0:26
• If all the points are within 30 meters of each other, just averaging the latitudes and longitudes will be very close to the "true" geodesic center for any reasonable definition of "center". At least as long as (i) you are not near the poles, and (ii) your points don't span across the 180° meridian.
– user856
Commented Dec 12, 2012 at 0:45
• Someone used the following method gist.github.com/3718961 and I wonder if this will work better than just averaging Commented Dec 12, 2012 at 0:51
• Sure, this avoids both problems in my previous comment, and is a reasonable definition of the centroid of several points on a sphere. What it's doing is finding the centroid in 3D space and then projecting it back onto the sphere.
– user856
Commented Dec 12, 2012 at 7:31

You have $n$ points on the globe, given as latitude $\phi_i$ and longitude $\lambda_i$ for $i=1,\ldots,n$ (adopting Wikipedia's notation). Consider a Cartesian coordinate system in which the Earth is a sphere centered at the origin, with $z$ pointing to the North pole and $x$ crossing the Equator at the $\lambda=0$ meridian. The 3D coordinates of the given points are \begin{align} x_i &= r\cos\phi_i\cos\lambda_i,\\ y_i &= r\cos\phi_i\sin\lambda_i,\\ z_i &= r\sin\phi_i, \end{align} (compare spherical coordinates, which uses $\theta=90^\circ-\phi$ and $\varphi=\lambda$). The centroid of these points is of course $$(\bar x,\bar y,\bar z) = \frac1n \sum (x_i, y_i, z_i).$$ This will not in general lie on the unit sphere, but we don't need to actually project it to the unit sphere to determine the geographic coordinates its projection would have. We can simply observe that \begin{align} \sin\phi &= z/r, \\ \cos\phi &= \sqrt{x^2+y^2}/r, \\ \sin\lambda &= y/(r\cos\phi), \\ \cos\lambda &= x/(r\cos\phi), \end{align} which implies, since $r$ and $r\cos\phi$ are nonnegative, that \begin{align} \bar\phi &= \operatorname{atan2}\left(\bar z, \sqrt{\bar x^2+\bar y^2}\right), \\ \bar\lambda &= \operatorname{atan2}(\bar y, \bar x). \end{align}