I am doing a project in PHP. I have a Restaurants table with LAT
and LONG
fields. I want to get 10 Restaurants (with specific cuisine_id
) whose distance is less than 5km from the USER
.
I am using the below function to calculate distance :
// Function for Getting distance between two coordinates.
function getDistance( $latitude1, $longitude1, $latitude2, $longitude2 ) {
$earth_radius = 6371;
$dLat = deg2rad( $latitude2 - $latitude1 );
$dLon = deg2rad( $longitude2 - $longitude1 );
$a = sin($dLat/2) * sin($dLat/2) + cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * sin($dLon/2) * sin($dLon/2);
$c = 2 * asin(sqrt($a));
$d = $earth_radius * $c;
return $d;
}
Is it possible to use the function while selecting records from database?
Can someone please tell me a sample query for getting (using as LIMIT
value ) Restaurants which are at a distance of 5km or less (User LAT
and LONG
are available) in one go (if possible using above mentioned getDistance
function).