Reverse geocoding is the process of converting geographical coordinates (latitude and longitude) into a human-readable address (street, city, postal code, country). This is useful in ride-hailing apps, delivery tracking, GPS logging, and real estate platforms. In this tutorial, we’ll integrate GeocoderLaravel with the Google Maps API to perform reverse geocoding in Laravel.
What You’ll Need
- Laravel project (v8+ recommended)
toin0u/geocoder-laravel
package installed- Google Maps Geocoding API Key
Step 1: Install GeocoderLaravel
composer require toin0u/geocoder-laravel
php artisan vendor:publish --provider="Geocoder\\Laravel\\Providers\\GeocoderService"
This will publish the configuration file config/geocoder.php
where you can manage providers.
Step 2: Configure Google Maps API Key
Inside your .env
, add your Google Maps Geocoding API key:
GOOGLE_MAPS_API_KEY=YOUR_GOOGLE_MAPS_API_KEY_HERE
Then, ensure config/geocoder.php
uses Google Maps:
return [
'providers' => [
Chain::class => [
GoogleMaps::class => [
env('GOOGLE_MAPS_LOCALE', 'us'),
env('GOOGLE_MAPS_API_KEY'),
],
GeoPlugin::class => [],
],
],
];
⚠️ Security Tip: Restrict your API key to specific domains or IPs inside the Google Cloud Console. This prevents abuse if your key leaks.
Step 3: Write Reverse Geocoding Logic
The following snippet takes latitude
and longitude
and returns the formatted address:
<?php
$latitude = 51.5291450;
$longitude = -0.1239401;
try {
$results = app('geocoder')->reverse($latitude, $longitude)->get();
if ($results->count() > 0) {
$address = $results[0]->getFormattedAddress();
echo "Address: " . $address;
} else {
echo "No address found for these coordinates.";
}
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}
Sample Output
16-18 Argyle Street, Camden, London WC1H 8EG, United Kingdom
You can call this method in a Controller, Job, or Service Class. For heavy usage (e.g., batch reverse geocoding), consider using queued jobs to avoid API delays blocking user requests.
Common Use Cases
- Convert GPS coordinates into user-friendly addresses in mobile apps.
- Show the nearest street or city when tracking delivery drivers.
- Store location history for auditing in HR or security management systems.
- Tag uploaded photos or assets with real-world addresses.
Conclusion
Reverse geocoding in Laravel becomes seamless with GeocoderLaravel + Google Maps API. With just a few lines of code, you can translate latitude and longitude into full addresses, improving user experience in location-based apps. Always monitor your API usage to control costs.