In this tutorial you will learn about the Laravel 9 Find Nearest Location By Latitude and Longitude and its application with practical example.
In this Laravel 9 Find Nearest Location By Latitude and Longitude tutorial I will show you How to find nearest location using latitude and longitude in laravel 9 application. In this tutorial you will learn How to find nearest location using latitude and longitude in laravel 9. In this article I will share example to find nearest location using latitude and longitude in laravel 9 application. When developing any web application we come to situations where we need to find the nearest location using latitude and longitude in laravel. In this tutorial I will help you find nearest location using your current latitude and longitude in laravel 9. In this example we will find neareby place using latitude and longitude query in laravel eloquent. You can get nearest geolocation by mysql radius query laravel 9.
Laravel 9 Find Nearest Location By Latitude and Longitude
In this step by step tutorial I will demonstrate you How to find nearest location using latitude and longitude in laravel 9. Please follow the instruction given below:
- Install Laravel 9
- Connecting App to Database
- Add Route
- Generate Controller by Command
- Run Development Server
- Test This App
Install Laravel 9
First of all we need to create a fresh laravel project, download and install Laravel 9 using the below command
1 |
composer create-project --prefer-dist laravel/laravel blog |
Connecting App to Database
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=Enter_Your_Database_Name DB_USERNAME=Enter_Your_Database_Username DB_PASSWORD=Enter_Your_Database_Password |
Add Route
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 |
use App\Http\Controllers\LocationController; Route::get('near-places', [LocationController::class, 'index']); |
Generate Controller by Command
Now, lets create a controller named LocationController using command given below –
1 |
php artisan make:controller LocationController |
After successfully create controller go to app/controllers/LocationController.php and update the following code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use DB; class LocationController extends Controller { // ---------------- [ Load View ] ---------------- public function index(Request $request) { $lat = YOUR_CURRENT_LATTITUDE; $lon = YOUR_CURRENT_LONGITUDE; $data = DB::table("users") ->select("users.id" ,DB::raw("6371 * acos(cos(radians(" . $lat . ")) * cos(radians(users.lat)) * cos(radians(users.lon) - radians(" . $lon . ")) + sin(radians(" .$lat. ")) * sin(radians(users.lat))) AS distance")) ->groupBy("users.id") ->get(); dd($data); } } |
Run Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 |
http://localhost:8000/near-places |