# Geolocation

Recall the **First Spherical Law of Cosines**:

Given a unit sphere, aspherical triangleon the surface of the sphere is defined by the great circles connecting three points $latex u$, $latex v$, and $latex w$ on the sphere. If the lengths of these three sides are $latex a$ (from $latex u$ to $latex v),$ $latex b$ (from $latex u$ to $latex w),$ and $latex c$ (from $latex v$ to $latex w),$ and the angle of the corner opposite $latex c$ is $latex C,$ then $$\cos c = \cos a \cos b + \sin a \sin b \cos C$$

In any decent device and for most computer languages, this formula should give well-conditioned results down to distances as small as around three feet, and thus can be used to compute an accurate geodetic distance between two given points in the surface of the Earth (well, ok, assuming the Earth is a *perfect sphere*). The geodetic form of the law of cosines is rearranged from the canonical one so that the latitude can be used directly, rather than the colatitude, and reads as follows: Given points $latex p_1$ and $latex p_2$ with positions $latex (lat_1, long_1)$ and $latex (lat_2, long_2)$ respectively, the distance $latex d$ between the two points is given by the following formula.

$$ \cos\displaystyle{\frac{d}{R_\oplus}}=\sin(lat_1)\sin(lat_2) + \cos(lat_1)\cos(lat_2)\cos(long_2-long_1),$$

where $latex R_\oplus=3,959$ is the radius of the Earth in miles (well, ok, the average radius of the Earth…)

A nice application of this formula can be used for **geolocation** purposes, and we recently had the pleasure to assist Thumb Mobile to write such functionality for one of their clients.

Go to www.lizardsthicket.com in your mobile device, and click on "Find a Location." This fires up the location services of your browser. When you accept, your latitude and longitude are tracked. After a fast, reliable and resource-efficient algorithm, the page offers the location of the restaurant from the Lizard's chain that is closest to you. Simple, right?