Laravel 10 How to Find Nearest Location By Latitude and Longitude ?

https://ahtesham.me/storage/posts/September2023/Af1588tH7AwEovS2wV66.jpg

In this tutorial, we will learn how to find the nearest location based on latitude and longitude using Laravel 10 in a simple and easy-to-follow manner. I’ll guide you through the steps to make use of Laravel 10’s latitude and longitude tools to determine the closest location. Specifically, we’ll focus on finding nearby locations in Laravel 10. Follow these straightforward steps to utilize Laravel 10 for locating the nearest place based on latitude and longitude.

Having access to latitude and longitude coordinates can be highly valuable for various purposes, such as identifying nearby businesses, residences, hotels, cities, and more. In this example, we’ll take a user-friendly approach. To enable location-based user searches by their closest latitude and longitude, we’ll add latitude and longitude fields to the users’ table.

Let’s get started with these easy-to-follow instructions:

 

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

<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\LocationController;
  
/*
|--------------------------------------------------------------------------
| 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']);	

Read More : Laravel 10 Multiple Authentications using Middleware Example

 

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

 

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…

Laravel News Links