spatie/geocoder
Convert any address into GPS coordinates in Laravel/PHP using Google’s Geocoding API. Simple facade-based calls return lat/lng plus accuracy, formatted address, and viewport data. Ideal for mapping, location search, and address validation.
Installation:
composer require spatie/geocoder
Publish the config file:
php artisan vendor:publish --provider="Spatie\Geocoder\GeocoderServiceProvider"
Configure API Key:
Edit config/geocoder.php and add your Google Maps API key under services.google_maps:
'google_maps' => [
'key' => env('GOOGLE_MAPS_API_KEY'),
],
Set the key in your .env:
GOOGLE_MAPS_API_KEY=your_api_key_here
First Use Case: Geocode an address in a controller or service:
use Spatie\Geocoder\Facades\Geocoder;
$coordinates = Geocoder::getCoordinatesForAddress('1600 Amphitheatre Parkway, Mountain View, CA');
// Returns: ['lat' => 37.422..., 'lng' => -122.084...]
Geocoder:: for quick access to methods.GeocoderServiceProvider.HasCoordinates for Eloquent models.Geocoding Addresses:
// Basic geocoding
$coordinates = Geocoder::getCoordinatesForAddress('Address, City, Country');
// With fallback to multiple providers
$coordinates = Geocoder::withFallbackTo([
'google_maps',
'openstreetmaps',
])->getCoordinatesForAddress('Address');
Reverse Geocoding:
$address = Geocoder::getAddressForCoordinates(37.422, -122.084);
Model Integration:
Use the HasCoordinates trait to auto-geocode addresses on model save:
use Spatie\Geocoder\HasCoordinates;
class Location extends Model
{
use HasCoordinates;
protected $geocodable = 'address';
}
Then call:
$location = new Location(['address' => '123 Main St']);
$location->geocode(); // Automatically populates lat/lng
Batch Processing:
$addresses = ['Address 1', 'Address 2'];
$coordinates = Geocoder::getCoordinatesForAddresses($addresses);
$coordinates = Geocoder::getCoordinatesForAddress('Address')->remember(60 * 24);
Spatie\Geocoder\Geocoder to add new providers.spatie/laravel-address).Geocoder::getCoordinatesForAddress('Address')->onQueue('geocoding');
API Key Restrictions:
billing key type in Google Cloud Console or switch to free alternatives like OpenStreetMap.Rate Limits:
Address Ambiguity:
getCoordinatesForAddressWithFallback().Model Trait Conflicts:
HasCoordinates may conflict with other traits or model events.geocode() or use explicit calls like $model->geocodeNow().Timeouts:
Log Responses:
Enable debug mode in config/geocoder.php:
'debug' => env('GEOCODER_DEBUG', false),
Logs will appear in storage/logs/geocoder.log.
Check HTTP Status:
Inspect the response property of the returned object:
$coordinates = Geocoder::getCoordinatesForAddress('Address');
if ($coordinates->hasError()) {
log($coordinates->getMessage());
}
Custom Geocoder:
Create a new provider by extending Spatie\Geocoder\Geocoder:
class CustomGeocoder extends Geocoder
{
public function getName()
{
return 'custom_provider';
}
public function getCoordinates($address)
{
// Custom logic
}
}
Register it in config/geocoder.php:
'providers' => [
'custom_provider' => \App\Services\CustomGeocoder::class,
],
Override Default Behavior: Bind your own geocoder instance in a service provider:
$app->bind(\Spatie\Geocoder\Geocoder::class, function ($app) {
return new \App\Services\CustomGeocoder();
});
Event Listeners:
Listen for geocoding events (e.g., Geocoded, GeocodingFailed) via Laravel's event system.
getCoordinatesForAddresses() instead of looping.lat/lng columns are indexed for spatial queries.spatie/laravel-scout-tntsearch for geospatial searches.How can I help you explore Laravel packages today?