I've a database table named "contractors" and the table is having 5 fields
Now I've an another lat and long against which I've to select these records and they are :
Latitude: 19.2494730 , Longitude : 72.8612790
My SQL query is :
SELECT *, round( (3959 * acos( cos( radians(19.2494730) ) * cos( radians( tbl.latitude ) ) * cos( radians( tbl.longitude ) - radians(72.8612790) ) + sin( radians(19.2494730) ) * sin( radians( tbl.latitude ) ) ) ),2) AS distance
FROM `contractors` AS tbl
and gives the following result :
But the distance (it is in Miles I think) is not correct because when I run the following JavaScript code, It gives me some accurate result.
function distance(lat1, lon1, lat2, lon2, unit) {
var radlat1 = Math.PI * lat1/180
var radlat2 = Math.PI * lat2/180
var radlon1 = Math.PI * lon1/180
var radlon2 = Math.PI * lon2/180
var theta = lon1-lon2
var radtheta = Math.PI * theta/180
var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
dist = Math.acos(dist)
dist = dist * 180/Math.PI
dist = dist * 60 * 1.1515
if (unit=="K") { dist = dist * 1.609344 }
if (unit=="N") { dist = dist * 0.8684 }
alert(dist)
}
distance(19.2494730, 72.8612790, 19.281085, 72.855994, 'K');
I also have a PHP snippet which gives me more accurate result
function distance($lat1, $lon1, $lat2, $lon2, $unit) {
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit = strtoupper($unit);
if ($unit == "K") {
return ($miles * 1.609344);
} else if ($unit == "N") {
return ($miles * 0.8684);
} else {
return $miles;
}
}
echo distance(19.2494730, 72.8612790,19.281085, 72.855994, "M") . " Miles<br>";
Could anybody help to make the above SQL query correct so that I can give me the accurate straight distance between all the areas and a particular Lat, Long.