laravel distance between coordinates code example

Example 1: laravel get distance between two locations

$sqlDistance = DB::raw('( 111.045 * acos( cos( radians(' . $data['lat'] . ') )        * cos( radians( addresses.latitude ) )        * cos( radians( addresses.longitude )        - radians(' . $data['lng']  . ') )        + sin( radians(' . $data['lat']  . ') )        * sin( radians( addresses.latitude ) ) ) )');

Example 2: laravel count distance lat/longtidue

$lat = 41.118491 // user's latitude
$lng = 25.404509 // user's longitude

SELECT *, 
( 6371 * acos( cos( radians($lat) ) 
* cos( radians( latitude ) ) 
* cos( radians( longitude ) - radians($lng) ) + sin( radians($lat) ) 
* sin( radians( latitude ) ) ) ) 
AS calculated_distance 
FROM settings as T 
HAVING calculated_distance <= (SELECT distance FROM settings WHERE sid=T.sid) 
ORDER BY distance_calc

Tags:

Php Example