Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a question, I am trying to calculate a radius between two latitude and longitude points on the Google map.

I understand the coding part, but I do not understand the mathematical side to it. I would appericate if you guys can give me some guidance to this.

Here is the code, maybe you can get some understanding on what I am trying to achive

( 3959 * acos( cos( radians(in_end_lat) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(in_end_lng) ) + sin( radians(in_end_lat) ) * sin( radians( lat ) ) ) )


share|cite|improve this question
You need to clarify your question. What do you mean by 'calculate a radius between to latitude and longitude points'? – copper.hat Apr 18 '12 at 17:44
I answered a similar question. I think you're looking for the great arc distance between two points on the sphere, in miles. – bgins Apr 18 '12 at 18:06

Latitude and longitude values are basically vectors in spherical coordinates, usually given without the radius of the Earth.

Let's say you have a 2D circle, and you have two vectors to the edge of that circle. You want to find the distance along the edge of the circle between the heads of those two vectors -- this is called the arc length. In order to do that, you need to know the angle $\beta$ between the two vectors and the radius $r$ of the circle:


An easy way to find the angle between two vectors is to use the dot product:

$$\cos \beta = \frac{\vec a \cdot \vec b}{|\vec a| |\vec b|}$$

This formula holds for vectors in 2-space as well as vectors in 3-space.

The goal now is to figure out what the two vectors should be in Cartesian coordinates. bgins' answer has this formula:

$$ X(\theta,\phi) =\left[\matrix{x\\y\\z\\}\right] =\left[ \matrix{ r\cos\phi\cos\theta\\ r\cos\phi\sin\theta\\ r\sin\phi }\right] =r\left[ \matrix{ \cos\phi\cos\theta\\ \cos\phi\sin\theta\\ \sin\phi }\right] $$

Now you just take two of these vectors and put them into the equation for the angle:

$$\vec a = r\left[ \matrix{ \cos\phi_a\cos\theta_a\\ \cos\phi_a\sin\theta_a\\ \sin\phi_a }\right]$$

$$\vec b = r\left[ \matrix{ \cos\phi_b\cos\theta_b\\ \cos\phi_b\sin\theta_b\\ \sin\phi_b }\right]$$

$$\vec a \cdot \vec b = r^2 (\cos\phi_a\cos\theta_a\cos\phi_b\cos\theta_b + \cos\phi_a\sin\theta_a\cos\phi_b\sin\theta_b + \sin\phi_a\sin\phi_b)$$

$$=r^2(\cos\phi_a\cos\phi_b[\cos\theta_a\cos\theta_b + \sin\theta_a\sin\theta_b] + \sin\phi_a\sin\phi_b)$$


It's easy to see that the magnitude of each vector is $r$, so the final formula is:

$$\cos \beta = \frac {r^2(\cos\phi_a\cos\phi_b\cos(\theta_a-\theta_b)+\sin\phi_a\sin\phi_b)}{r^2}$$

$$\beta = \cos^{-1}(\cos\phi_a\cos\phi_b\cos(\theta_a-\theta_b)+\sin\phi_a\sin\phi_b)$$


share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.