<p>The following is accurate if the points are within 10000 km of each
other and if the earth is assumed to be spherical.  Use d2 if you just
want to compare distances.  distance is the approximate distance in km.</p>

<pre><code>deg = pi/180;
phi1 = lat1 * deg;
phi2 = lat2 * deg;
lam12 = (lon2 - lon1) * deg;

d2 = ( cos(phi1) * sin(phi2) - sin(phi1) * cos(phi2) * cos(lam12) )^2
 + ( cos(phi2) * sin(lam12) )^2;

a = 6371.009;  // kilometers
distance = a * asin( sqrt( d2 ) );
</code></pre>

<p>For more accuracy you need to treat the earth as an ellipsoid; see
my online geodesic calculator at
<a href="http://geographiclib.sf.net/cgi-bin/Geod" rel="nofollow">http://geographiclib.sf.net/cgi-bin/Geod</a> and the write-up at
<a href="http://arxiv.org/abs/1102.1215" rel="nofollow">http://arxiv.org/abs/1102.1215</a>.</p>
Latitude/Longitude distance question
<p>I'm working on a tool that will find the closest latitude/longitude position from a list compared to the users current location.  The list will be pretty long, and it will be running on a smart phone, so I'd like to make the calculations as simple and speedy as possible.  From the threads I've read, calculating a fairly accurate distance between two latitude/longitude locations is a bit complicated, and I'm worried about speed.  My question is, can I use something similar to the following to get a reasonable result for closest list position and use that?</p>

<pre><code>$distance = sqrt((($firstLongitude-$secondLongitude)*($firstLongitude-$secondLongitude))+(($firstlLatitude-$secondLatitude)*($firstLatitude-$secondLatitude)));
</code></pre>

<p>I know the example is in PHP, but the logic should be evident.  So my question is, will using the above logic to determine the closest location to the user from a list of lat/long locations give me correct results, or is there a potential issue I'm missing?  </p>

<p>This app would only run for locations in the USA, if that makes any difference.</p>

<p>NOTE:
I was also wondering if I could further simplify this and drop the sqrt part since I just want to see which is closer, and not how close it is.</p>