creativefactoryrv/google-maps-geocoder

Simple and lightweight geocoder that uses the Google Maps Platform API. It can optionally use a PSR cache.

1.0.3 2022-09-28 16:08 UTC

This package is auto-updated.

Last update: 2024-04-18 10:53:32 UTC


README

A super simple geocoding class that use Google Maps Platform to do the magic.
You only need to obtain a Google Maps Platform API (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-Maps-Platform-API-Key-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-Maps-Platform-API-Key-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-Maps-Platform-API-Key-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-Maps-Platform-API-Key-XXXXXXXXX', 'en');
$location = $geocoder->query('Duomo, Milano, Italy');
$myArray = $location->getCoordinates();

Country

Returns the country value.

$geocoder = new Geocoder('XXXXXXXXX-My-Google-Maps-Platform-API-Key-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-Maps-Platform-API-Key-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-Maps-Platform-API-Key-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-Maps-Platform-API-Key-XXXXXXXXX', 'en');
$location = $geocoder->query('Duomo, Milano, Italy');
echo $location->getRoute();

Street number

Returns the street number value.

$geocoder = new Geocoder('XXXXXXXXX-My-Google-Maps-Platform-API-Key-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-Maps-Platform-API-Key-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-Maps-Platform-API-Key-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-Maps-Platform-API-Key-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-Maps-Platform-API-Key-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-Maps-Platform-API-Key-XXXXXXXXX', 'en');
$location = $geocoder->query('Duomo, Milano, Italy');
$format = '%R %N, %C';
echo $location->format($format);

License

MIT