I have below two Driver_position
and user_position
table where user and driver lat/long position are being updated. I Need to findout driver_id's for each user_id who are near to 5km of user_position , so that result will be like user_id1 = driver_id1,driver_id2,driver_id3 etc
For user_id2=driver_id5
So far I have below code to calculate distance in km. How can I achieve above in PHP
$lat1 = deg2rad($user_latitude);
$lon1 = deg2rad($user_longitude);
$lat2 = deg2rad($driver_latitude);
$lon2 = deg2rad($driver_longitude);
$latDelta = $lat1 - $lat2;
$lonDelta = $lon1 - $lon2;
$earthRadius=3959;
$angle = 2 * asin(sqrt(pow(sin($latDelta / 2), 2) +
cos($lat2) * cos($lat1) * pow(sin($lonDelta / 2), 2)));
//return $angle * $earthRadius;
$dis= ($angle * $earthRadius)/0.62137; // miles to km conversion mi / 0.62137 = km
You can use below Mysql query for your solutions
// To search by kilometers instead of miles, replace 3959 with 6371
SELECT user_position.*,driver_position.driver_id, ( 3959 * acos( cos( radians(37.4219983) ) * cos( radians( driver_position.latitude ) ) * cos( radians( driver_position.longitude ) - radians(-122.0844) ) + sin( radians(37.4219983) ) * sin(radians(driver_position.latitude)) ) ) AS distance FROM user_position ,driver_position where user_position.user_id=10002 HAVING distance < 10000
Hope this will helps you.