some notes

Finding Distance Using Lat/Long

Daniel Kushner just copied-and-pasted some useful Trig -- given a latitude and longitude, how do you find the distance in miles between them?

I've never worked on a travel site, but I *am* interested in proximity. And I don't remember much trigonometry, thanks anyway, Coach Rau.

Spot,

Here's some code that I have from a long time ago that calculates the distance between airports based on longitude and latitude. You can do the same with Zip codes once you convert a zip code to long/lat.

Best,
Daniel Kushner

$db = new My_DB("SELECT *
FROM airports AS a
WHERE id IN ('$origin', '$destination')");

$db->next_record();
$a = deg2rad($db->f('latitude'));
$b = deg2rad($db->f('longitude'));
$origin_id = $db->f('id');
$origin_name = $db->f('name');

$db->next_record();
$c = deg2rad($db->f('latitude'));
$d = deg2rad($db->f('longitude'));
$destination_id = $db->f('id');
$destination_name = $db->f('name');


$r=3963.1; //radius of the earth in miles

//calculate the distance between the two points
$distance = acos(
(cos($a) * cos($b) * cos($c) * cos($d)) +
(cos($a) * sin($b) * cos($c) * sin($d)) +
(sin($a) * sin($c))
) * $r;

if($origin == $destination) {
echo "Distance = 0
";
} else {
echo "Origin: ($origin_id) $origin_name
";
echo "Destination: ($destination_id) $destination_name
";
echo "Distance in miles: ", round($distance,4), "
";
echo "Distance in kilometers: ", round(($distance*1.609),4), "
";
}

-----Original Message-----
From: talk-bounces@lists.nyphp.org talk-bounces@lists.nyphp.orgOn Behalf Of Spot
Sent: Thursday, November 20, 2003 2:56 PM
To: talk@lists.nyphp.org
Subject: [nyphp-talk] Hello / Geo coords

Does anyone have any information on methods for creating "crow flies" distances between artists based on geo coords? I would be interested in using a third party library if there is one.


Thanks!

Spot
Director of Prints
deviantART Inc.
www.deviantart.com



Yeah, and Spot I'm right there with ya -- we have these brilliant tools for making the Internet a local phenomenon, it would be great to come up with libraries that made it easy.

The possibilities are hardly being realised over at GeoURL, unless I'm missing something.

By Chris Snyder on November 21, 2003 at 12:02am

Source: nyphp talk

jump to top