Laravel 9 Find Nearest Location By Latitude and Longitude Tutorial

In this tutorial we will see how to find the nearest location by laravel 9 lat and long. If you have locations, homes, cities, states, countries, dealers, products, suppliers, hotels, etc. with latitude and longitude, you can easily find them near you. If you want to see an example of how to find the nearest location using latitude and longitude in Laravel 9 then you are at the right place.

Here we will create a simple example. We'll add latitude and longitude to the users' table and see how to find users by nearby latitude and longitude. For which follow the steps below

Step 1: Create Laravel App :

If you already have a newer app, you need to get the latest version of the application using the Belo command, run the following command

composer create-project laravel/laravel nearest-location

Step 2: Create Route :

Here we create a way to get the nearest location from the latitude and long example

Goto routes/web.php file and edit bellow code

<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\AreaController;
  
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
  
Route::get('near-by-places', [AreaController::class, 'index']);

Step 3: Create Controller :

Now we need an area controller so run the following command to create a area controller

php artisan make:controller AreController

Goto app/Http/Controllers/AreController.php file and you have users table with lat and long columns. also add some dummy records on table as well.

app/Http/Controllers/AreController.php

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Model\User;
  
class LocationController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $lat = YOUR_CURRENT_LATTITUDE;
        $lon = YOUR_CURRENT_LONGITUDE;
           
        $users = User::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($users);
    }
}

Step 4: Run Laravel App :

This is the last step in which we run Laravel App. Now you have to type the following command and press enter to run the app:

php artisan serve

Now, Go to your web browser, type the given URL and view the app output:

http://localhost:8000/near-by-places

I hope this will help you to understand Find Nearest Location By Latitude and Longitude in laravel app