If you have Latitude and Longitude stored in your MySQL database and you want to get the nearest location in laravel. So In this tutorial, you will learn how to get the nearest location using latitude and longitude in laravel 10 with query builder.
How to Find Nearest Location using Latitude and Longitude in Laravel 10
By using these steps, you can get/find the nearest location or places using latitude and longitude in Laravel:
- Step 1 – Create New Laravel 10 Project
- Step 2 – Setup Database with Laravel App
- Step 3 – Add Routes
- Step 4 – Create Controller File
- Step 5 – Start Application Development Server
- Step 6 – Test This App
Step 1 – Create New Laravel 10 Project
Firstly, Open your terminal or command prompt.
Then execute the following command on the terminal to install/download Laravel 10 application in your system(server):
composer create-project --prefer-dist laravel/laravel blog
Step 2 – Setup Database with Laravel App
In this step, Open .env file, which is located on root directory of your laravel applications.
So, visit your app root directory and find .env file. Then configure database details as follows:
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
Step 3 – Add Routes
In this step, create routes in the web.php file.
So, Go to app/routes/web.php file and following routes in the web.php file:
use App\Http\Controllers\LocationController; Route::get('near-places', [LocationController::class, 'index']);
Step 4 – Create Controller File
In this step, open your terminal or command prompt.
Then execute the following command on the terminal to create a controller name LocationController. So you need to use the below command and create a Controller:
php artisan make:controller LocationController
Once you have successfully created the controller file. Then go to app/controllers/LocationController.php and update the following code:
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use DB; class LocationController extends Controller { // ---------------- [ Location ] ---------------- 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); } }
Step 5 – Start Application Development Server
In this step, execute the following command on the terminal to start the development server. Use the php artisan serve command and start your server:
php artisan serve
If you want to run the project diffrent port so use this below command
php artisan serve --port=8080
Step 6 – Test This App
Open your browser and hit the following url on it:
http://localhost:8000/near-places
Conclusion
In this google place autocomplete in laravel tutorial, you have learned how to implement google autocomplete web application using google place APIs in laravel.
How are you fetching the user lat and log