# Different ways for calculating distance between two geodetic points give me different results

I'm trying to calculate the distance between two geodetic points in two different ways. The points are:

A:(41.466138, 15.547839)
B:(41.467216, 15.547025)


The distance between the two points is pretty small (about 130/140 meters)

1) I have used the Haversine forumla that using this site gives me the following result: So the mesured distance using the Haversine formula is 137,7 meters.

2) I have tried to calculate the distance transformating the geodetic coordinates (latitude, longitude) in cartesian coordinates (x, y). I have calculated the (x,y) coordinates for A and B:

R =  6371; //radius of the earth
X = R * Math.cos(lat) * Math.cos(lon); // lon and lat are in radiants (which is not the same as provided above, must multiply by Pi / 180)
Y = R * Math.cos(lat) * Math.sin(lon);


So I obtained:

A: (4599,392641683213, 1279,6610297820203)
B: (4599,334350798723, 1279,574411410788)


NOTE: It assumes that the altitude is negligible in A and B.

Now that I have the cartesian coordinates I can calculate the distance using Pitagora's theorem, so:

|Xb - Xa| = 4599,392641683213 - 4599,334350798723 = 0,05829 Km
|Yb - Ya| = 1279,6610297820203 - 1279,574411410788 = 0,08662 Km

distanceAB = sqrt(0,05829^2 + 0,08662^2) = 0,1044 Km = 104,4 m


As you can see the distance calculated with the above methods generate different values:

137,7 m for with the former, 104,4 m with the latter.

Why this happen?

• Note that your X/Y equations above are the same (both are cos & cos), but your computed values are not, so you probably have some errors there. – Mark Ping Jan 13 '14 at 17:02
• Calculate angle $\theta$ between vectors. Geodesic is $\left\vert\,R\theta\,\right\vert$. – Felix Marin Feb 16 '14 at 23:17

The Haversine formula gives the "as-the-crow-flies" distance, i.e., the great circle distance along the surface of the earth. If you take the Euclidean distance between two points in $\mathbb{R}^3$, you are finding the straight-line distance, which will cut through the earth. That being said, if the points are close together, the results should be very similar: the discrepancy in your case is due to another mistake you made, which is ignoring the $z$-coordinate in the Euclidean calculation. The $z$-coordinate doesn't correspond to the altitude; it's the altitude at the North Pole, but at the equator it's the north-south direction. Using $$\begin{eqnarray} x &=& R\cos\theta\cos\phi \\ y &=& R\cos\theta\sin\phi \\ z &=& R\sin\theta, \end{eqnarray}$$ you find $$\begin{eqnarray} (x_A,y_A,z_A) &=& (4599.39264, 1279.66103, 4218.73156) \\ (x_B,y_B,z_B) &=& (4599.33435, 1279.57441, 4218.82138); \end{eqnarray}$$ and the Euclidean distance between them is indeed $137.7$m.
• Could you help me if i'd like to get the distance with a height? So I have coordinates with the data of height in that point. If I use this formula, but add the height to the R in the 3rd (z), will that be OK? I mean z = (R+height)*sin theta. – androbin Mar 30 '17 at 7:49
• @androbin: you would replace $R$ with $R+h$ in all three coordinates. – mjqxxxx Mar 30 '17 at 14:09