Find Nearest Location By Latitude and Longitude in Laravel 6
In this article, we can display the Nearest Location using the google map of Latitude and Longitude in laravel 6.
If you want to display a specific place according to a nearby place then you can display the Nearest Location using the below following example.
SQL Query
1 2 3 4 5 6 7 8 9 | 6371 * acos(cos(radians(" . $lat . ")) * cos(radians(orders.latitude)) * cos(radians(orders.longitude) - radians(" . $lon . ")) + sin(radians(" .$lat. ")) * sin(radians(orders.latitude))) AS distance |
Example
1 2 3 4 5 6 7 8 9 10 11 12 | $lat = YOUR_CURRENT_LATTITUDE; $lon = YOUR_CURRENT_LONGITUDE; DB::table("orders") ->select("orders.id" ,DB::raw("6371 * acos(cos(radians(" . $lat . ")) * cos(radians(orders.latitude)) * cos(radians(orders.longitude) - radians(" . $lon . ")) + sin(radians(" .$lat. ")) * sin(radians(orders.latitude))) AS distance")) ->groupBy("orders.id") ->get(); |
Please follow and like us: