creativefactoryrv / google-maps-geocoder
Simple and lightweight geocoder that uses the Google Maps Platform API. It can optionally use a PSR cache.
Requires
- php: >=8.0
- psr/cache: ^3.0
Requires (Dev)
- symfony/cache: 7.*
README
A super simple geocoding class that use Google Maps Platform to do the magic.
You only need to obtain a Google Geocoding API key (https://developers.google.com/maps).
Optionally, the class can leverage a PRS-6 cache implementation in order to query the Maps Platform only when necessary.
Install
composer require creativefactoryrv/google-maps-geocoder
How to use
$geocoder = new Geocoder('XXXXXXXXX-My-Google-Geocoding-API-XXXXXXXXX', 'en'); $location = $geocoder->query('Duomo, Milano, Italy'); $latLng = $location->getCoordinates(); echo 'Lat: ' . $latLng['lat'] . ' / Lng: ' . $latLng['lng']; echo 'ZIP code: ' . $location->getPostalCode();
Get a JSON
Return the Google Maps Platform API response as it is (JSON).
$geocoder = new Geocoder('XXXXXXXXX-My-Google-Geocoding-API-XXXXXXXXX', 'en'); $location = $geocoder->query('Duomo, Milano, Italy'); $myJSONString = $location->getRaw();
Get an array
Return the Google Maps Platform API response as an associative array.
$geocoder = new Geocoder('XXXXXXXXX-My-Google-Geocoding-API-XXXXXXXXX', 'en'); $location = $geocoder->query('Duomo, Milano, Italy'); $myArray = $location->toArray();
Get latitude and longitude
Returns an array with the desired values.
$geocoder = new Geocoder('XXXXXXXXX-My-Google-Geocoding-API-XXXXXXXXX', 'en'); $location = $geocoder->query('Duomo, Milano, Italy'); $myArray = $location->getCoordinates();
Country
Returns the country value.
$geocoder = new Geocoder('XXXXXXXXX-My-Google-Geocoding-API-XXXXXXXXX', 'en'); $location = $geocoder->query('Duomo, Milano, Italy'); echo $location->getCountry();
Locality (city)
Returns the city or locality value.
$geocoder = new Geocoder('XXXXXXXXX-My-Google-Geocoding-API-XXXXXXXXX', 'en'); $location = $geocoder->query('Duomo, Milano, Italy'); echo $location->getLocality();
Postal code
Returns the postal code or zipcode value.
$geocoder = new Geocoder('XXXXXXXXX-My-Google-Geocoding-API-XXXXXXXXX', 'en'); $location = $geocoder->query('Duomo, Milano, Italy'); echo $location->getPostalCode();
Route (street name)
Returns the street name (called "route" by Google).
$geocoder = new Geocoder('XXXXXXXXX-My-Google-Geocoding-API-XXXXXXXXX', 'en'); $location = $geocoder->query('Duomo, Milano, Italy'); echo $location->getRoute();
Street number
Returns the street number value.
$geocoder = new Geocoder('XXXXXXXXX-My-Google-Geocoding-API-XXXXXXXXX', 'en'); $location = $geocoder->query('Duomo, Milano, Italy'); echo $location->getStreetNumber();
Administrative levels 1
Returns the administrative levels 1 value.
$geocoder = new Geocoder('XXXXXXXXX-My-Google-Geocoding-API-XXXXXXXXX', 'en'); $location = $geocoder->query('Duomo, Milano, Italy'); echo $location->getAdministrativeAreaLevel1();
Administrative levels 2
Returns the administrative levels 2 value.
$geocoder = new Geocoder('XXXXXXXXX-My-Google-Geocoding-API-XXXXXXXXX', 'en'); $location = $geocoder->query('Duomo, Milano, Italy'); echo $location->getAdministrativeAreaLevel2();
Administrative levels 3
Returns the administrative levels 3 value.
$geocoder = new Geocoder('XXXXXXXXX-My-Google-Geocoding-API-XXXXXXXXX', 'en'); $location = $geocoder->query('Duomo, Milano, Italy'); echo $location->getAdministrativeAreaLevel3();
Formatted address
Returns a string representing the address (called "formatted_address" by Google).
$geocoder = new Geocoder('XXXXXXXXX-My-Google-Geocoding-API-XXXXXXXXX', 'en'); $location = $geocoder->query('Duomo, Milano, Italy'); echo $location->getFormattedAddress();
Format as you like
Returns a formatted address according to $format.
- %A1 Administrative Area Level 1
- %a1 Administrative Area Level 1 (short version)
- %A2 Administrative Area Level 2
- %a2 Administrative Area Level 2 (short version)
- %A3 Administrative Area Level 3
- %a3 Administrative Area Level 3 (short version)
- %C Country
- %L Locality
- %P Postal code
- %R Route
- %N Street number
$geocoder = new Geocoder('XXXXXXXXX-My-Google-Geocoding-API-XXXXXXXXX', 'en'); $location = $geocoder->query('Duomo, Milano, Italy'); $format = '%R %N, %C'; echo $location->format($format);
License
MIT