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.
composer require creativefactoryrv/google-maps-geocoder$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();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();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();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();Returns the country value.
$geocoder = new Geocoder('XXXXXXXXX-My-Google-Geocoding-API-XXXXXXXXX', 'en');
$location = $geocoder->query('Duomo, Milano, Italy');
echo $location->getCountry();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();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();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();Returns the street number value.
$geocoder = new Geocoder('XXXXXXXXX-My-Google-Geocoding-API-XXXXXXXXX', 'en');
$location = $geocoder->query('Duomo, Milano, Italy');
echo $location->getStreetNumber();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();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();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();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();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);MIT