Laravel 9 Find Nearest Location By Latitude and Longitude Example

Hi Dev,

This tutorial shows you laravel 9 find nearest location by lat and long. we will help you to give example of find closest location with longitude and latitude laravel 9. if you want to see example of how to find near location using latitude and longitude in laravel 9 then you are a right place. you’ll learn laravel 9 find near by lat and long.

If you have places, hotels, homes, cities, states, countries, products, dealers, suppliers, etc with latitude and longitude locations then you can simply find them easily nearby you. I will give you a simple example. we will add lat and long on the users table and find users by the nearest latitude and longitude.

Let’s follow the below step to do this example.

Laravel 9 Find Nearest Location By Latitude and Longitude Example

Step 1: Install Laravel

first of all we need to get fresh Laravel version application using bellow command, So open your terminal OR command prompt and run bellow command:

composer create-project laravel/laravel blog

Step 2: Create Route

In this is step we need to create one route for getting near location from lat and long example.

routes/web.php

.medrectangle-4-multi-155{border:none!important;display:block!important;float:none;line-height:0;margin-bottom:15px!important;margin-left:0!important;margin-right:0!important;margin-top:15px!important;max-width:100%!important;min-height:250px;min-width:300px;padding:0;text-align:center!important}

<?php

use IlluminateSupportFacadesRoute;

use AppHttpControllersLocationController;

/*

|--------------------------------------------------------------------------

| 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', [LocationController::class, 'index']);

Step 3: Create Controller

in this step, we need to create LocationController and add following code on that file. you have users table with lat and long columns. also add some dummy records on table as well.

app/Http/Controllers/LocationController.php

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;

use AppModelUser;

class LocationController extends Controller

{

/**

* Display a listing of the resource.

*

* @return IlluminateHttpResponse

*/

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);

}

}

Run Laravel App:

All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel 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 it can help you…

Leave a Comment