Query to select records from your data base on the bases of latitude and longitude and calculate distance in miles

I have latitude and longitude.Now i need a query to select  all select records from my data base between these two on 100 miles radius. So now i write below query to perform my operation :

Example :

$latitude="27.950898" ;
$longitude= "-82.461517" ;

function getDataByRadius($latitude, $longitude, $radius = 5  ){

$query= "SELECT pop.* ,( 3959 * acos( cos( radians('".$latitude."') ) * cos( radians( pop.lat_decimal ) ) * cos( radians( pop.long_decimal ) - radians('".$longitude."') ) + sin( radians('".$latitude."') ) * sin( radians( pop.lat_decimal ) ) ) ) AS distance FROM mw_popsite AS pop HAVING distance < '".$radius."'  ORDER BY distance ASC ";

$query_exe= mysql_query($query);    
 return $data;


Description : In  this function my table name is  "mw_popsite" and i want to select all records which latitude and longitude between given latitude and longitude.So i make this simple function in  which you have to pass three parameter latitude,longitude and radius(This distance is in miles.This is count from the center point of any country on google map ).It will return you all data in array form with it distance (In miles) .

If you have any query regarding this then please comment .

Happy coding :) 

Post a Comment

Previous Post Next Post