Lasse Reichstein Nielsen wrote on 10 okt 2008 in comp.lang.javascript

:

> Sunny <(E-Mail Removed)> writes:

>

>> do someone know, How we can find the smallest distance between a bunch

>> of lat 7 long?

>> Like I have 10 Latitude & Longitude.

>

> ... some coordinates on the earth ...

>

>> If I want to see, which one is closer to the first point.

>> How should I do?

>

> Run through them, one by one, calculate the distance to the first

> point, and remember only the closest one.

> You might need http://en.wikipedia.org/wiki/Great_circle_distance
<http://williams.best.vwh.net/avform.htm#LL>

<http://www.movable-type.co.uk/scripts/latlong.html>

I tried it this way:

=========================================

<script type='text/javascript'>

function GreatCircleDistance(lat1,lon1,lat2,lon2) {

// convert to radians

lat1 = lat1 * Math.PI / 180;

lon1 = lon1 * Math.PI / 180;

lat2 = lat2 * Math.PI / 180;

lon2 = lon2 * Math.PI / 180;

// based on earth radius of 6371 km

// Expects the earth to be a perfect sphere.

// returns km

return 6371 *

Math.acos(

Math.sin(lat1) * Math.sin(lat2) +

Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2-lon1)

);

};

// test

alert(GreatCircleDistance(52,4,52,6)); // local distance

alert(GreatCircleDistance(0,0,180,0)); // halve around the equator

alert(GreatCircleDistance(0,0,0,1)/60); // 1 nautical mile

alert(GreatCircleDistance(0,0,0,1/60)); // 1 nautical mile

</script>

=========================================

--

Evertjan.

The Netherlands.

(Please change the x'es to dots in my emailaddress)