Introduction:
In Laravel 10, finding the nearest location based on latitude and longitude is a common requirement, especially in applications involving location-based services. Whether you’re building a mapping application or a service that requires proximity calculations, Laravel provides a convenient way to implement this functionality. In this article, we will walk through the steps to find the nearest location using latitude and longitude coordinates, accompanied by practical and easy-to-understand code examples.
Step 1: Set Up Your Laravel Project:
If you haven’t already, create a new Laravel project using the following command:
composer create-project --prefer-dist laravel/laravel nearest-location-example
Navigate into your project directory:
cd nearest-location-example
Step 2: Install a Laravel Package:
To simplify the process of calculating distances between locations, we’ll use the “matriphe/nearest-geolocation” package. Install it via Composer:
composer require matriphe/nearest-geolocation
Step 3: Create a Model and Migration:
Let’s create a model and migration for our locations. In this example, we’ll assume you have a “locations” table with columns for latitude and longitude.
php artisan make:model Location -m
Open the generated migration file (located in the database/migrations directory) and modify the up
method to include latitude and longitude:
public function up()
{
Schema::create('locations', function (Blueprint $table) {
$table->id();
$table->double('latitude');
$table->double('longitude');
$table->timestamps();
});
}
Run the migration to create the “locations” table:
php artisan migrate
Step 4: Seed the Database:
For testing purposes, let’s seed the database with a few sample locations. Open the LocationSeeder.php
file in the database/seeders directory and add the following code:
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class LocationSeeder extends Seeder
{
public function run()
{
DB::table('locations')->insert([
['latitude' => 40.7128, 'longitude' => -74.0060], // New York City
['latitude' => 34.0522, 'longitude' => -118.2437], // Los Angeles
['latitude' => 51.5074, 'longitude' => -0.1278], // London
// Add more locations as needed
]);
}
}
Run the seeder:
php artisan db:seed --class=LocationSeeder
Step 5: Create a Controller:
Generate a controller to handle location-related operations:
php artisan make:controller LocationController
Open the generated controller (located in the app/Http/Controllers directory) and add the following methods:
use Illuminate\Http\Request;
use Matriphe\Geolocation\Geolocation;
class LocationController extends Controller
{
public function index()
{
return view('locations.index');
}
public function findNearest(Request $request)
{
$latitude = $request->input('latitude');
$longitude = $request->input('longitude');
$locations = Location::all(); // Replace with your actual model name
$nearestLocation = Geolocation::find($latitude, $longitude, $locations);
return view('locations.nearest', compact('nearestLocation'));
}
}
Step 6: Create Views:
Create Blade views to display the input form and the nearest location. In the resources/views directory, create two files: index.blade.php and nearest.blade.php.
index.blade.php:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Find Nearest Location</title>
</head>
<body>
<form action="{{ route('find-nearest') }}" method="post">
@csrf
<label for="latitude">Latitude:</label>
<input type="text" name="latitude" required>
<br>
<label for="longitude">Longitude:</label>
<input type="text" name="longitude" required>
<br>
<button type="submit">Find Nearest Location</button>
</form>
</body>
</html>
nearest.blade.php:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Nearest Location</title>
</head>
<body>
<h1>Nearest Location</h1>
<p>Latitude: {{ $nearestLocation->latitude }}</p>
<p>Longitude: {{ $nearestLocation->longitude }}</p>
</body>
</html>
Step 7: Define Routes:
Open the routes/web.php file and add the following code to define the routes for the LocationController:
use App\Http\Controllers\LocationController;
Route::get('/', [LocationController::class, 'index']);
Route::post('/find-nearest', [LocationController::class, 'findNearest'])->name('find-nearest');
Step 8: Test the Application:
Run your Laravel development server:
php artisan serve
Visit http://localhost:8000 in your browser, and you should see the input form. Enter latitude and longitude values, submit the form, and you’ll be directed to a page displaying the nearest location.
Conclusion:
Implementing the functionality to find the nearest location in Laravel 10 using latitude and longitude coordinates is a valuable feature for location-based applications. With the help of the “matriphe/nearest-geolocation” package, you can easily perform distance calculations and provide users with relevant information based on their geographical proximity. By following the steps outlined in this article, you can incorporate this functionality into your Laravel projects and enhance the user experience in applications requiring location-aware features.
- Check our tools small Tools
- Check our tools website Word count