geocoder-php/google-maps-provider
Google Maps provider for the PHP Geocoder library. Adds geocoding and reverse geocoding via Google’s APIs, supporting address and coordinate lookups with configurable options and authentication. Suitable for Laravel and other PHP apps.
Installation
composer require geocoder-php/google-maps-provider
Requires geocoder-php/geocoder (≥v3.0) as a dependency.
Basic Usage
use Geocoder\Geocoder;
use Geocoder\Provider\GoogleMaps\GoogleMaps;
$geocoder = new Geocoder();
$geocoder->registerProvider(new GoogleMaps('YOUR_API_KEY'));
// Geocode an address
$results = $geocoder->geocode('1600 Amphitheatre Parkway, Mountain View, CA');
foreach ($results as $hit) {
echo $hit->getCoordinates(); // Lat/Lng
}
First Use Case
$results = $geocoder->reverseGeocode([37.422, -122.084]);
Address Validation
$geocoder->geocode('Invalid Address')->isEmpty() // true/false
Batch Processing
$addresses = ['addr1', 'addr2'];
$results = $geocoder->geocodeBatch($addresses);
Caching Responses (via Geocoder\Provider\Cache)
$cache = new \Geocoder\Provider\Cache\DoctrineCache();
$geocoder->registerProvider(new GoogleMaps('API_KEY', [], $cache));
Integration with Laravel
// In a service provider
$this->app->singleton(Geocoder::class, function ($app) {
$geocoder = new Geocoder();
$geocoder->registerProvider(new GoogleMaps(config('services.google_maps.key')));
return $geocoder;
});
Custom Request Options (e.g., language, region bias):
$provider = new GoogleMaps('API_KEY', [
'language' => 'fr',
'region' => 'fr'
]);
Place Autocomplete (via GoogleMaps::autocomplete()):
$results = $geocoder->autocomplete('Paris', ['types' => 'geocode']);
API Key Restrictions
Rate Limits
OVER_QUERY_LIMIT errors.Deprecated Methods
GoogleMaps::geocodeQuery() (use geocode() directly).getCoordinates() over getPosition() (deprecated in v4+).SSL Requirements
APP_URL accordingly.$provider = new GoogleMaps('API_KEY', [], [], true); // $debug = true
try {
$results = $geocoder->geocode('...');
} catch (\Geocoder\Exception\UnsupportedProviderException $e) {
// Fallback to another provider
}
Custom Response Parsing
Override GoogleMaps::parse() to handle non-standard API responses.
Middleware for Requests
Extend GoogleMaps and inject middleware (e.g., for auth headers):
class CustomGoogleMaps extends GoogleMaps {
protected function createClient() {
$client = parent::createClient();
$client->setDefaultOption('headers', ['X-Custom-Header' => 'value']);
return $client;
}
}
Laravel Service Provider Bind the provider to the container with config:
$this->app->bind(GoogleMaps::class, function ($app) {
return new GoogleMaps(config('services.google_maps.key'));
});
geocodeBatch() for >10 addresses (reduces overhead).How can I help you explore Laravel packages today?