Archive for May, 2008


After doing a bit of research on the web, I was able to find exactly what I was looking for. At first I looked at how to calculate distance from 2 latitude and longitude points mathematically but then tried to find out if anyone else had found a solution using MySql or PHP that I could reuse. Have a look at the solution below.

PHP:

function getDistanceBetweenPointsNew($latitude1, $longitude1, $latitude2, $longitude2, $unit = 'Mi') {
$theta = $longitude1 - $longitude2;
$distance = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) +
(cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) *
cos(deg2rad($theta)));
$distance = acos($distance);
$distance = rad2deg($distance);
$distance = $distance * 60 * 1.1515;
switch($unit) {
case ‘Mi’: break;
case ‘Km’ : $distance = $distance * 1.609344;
}return (round($distance,2));
}

MySQL:

$qry = "SELECT *,(((acos(sin((".$latitude."*pi()/180)) * sin((`Latitude`*pi()/180))+cos((".$latitude."*pi()/180)) * cos((`Latitude`*pi()/180)) * cos(((".$longitude."- `Longitude`)*pi()/180))))*180/pi())*60*1.1515) as distance FROM `MyTable` WHERE distance <= ".$distance."

Now I wanted to make sure that others are able to benefit from the help that the author of The marketing technology blog provide me.

 

Hows that for a chunk of change!

Check out the full story here over at BizJournals.com