# How to calculate center point of geo coordinates?

How can I calculate center point of N lat/lon pairs, if the distance is at 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. –  Henning Makholm Dec 12 '12 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. –  Pablo Dec 12 '12 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. –  Rahul Dec 12 '12 at 0:45
Someone used the following method gist.github.com/3718961 and I wonder if this will work better than just averaging –  Pablo Dec 12 '12 at 0:51
@Rahul Narain you can answer so I can accept it, if you would like. –  Pablo Dec 12 '12 at 10:14

For completeness (I know this is pretty late; no need to change your accepted answer):

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}

So yes, the code you linked to does appear to switch the latitude and longitude in the output. You should submit a patch to the author.

-