项目作者: chroma-x

项目描述 :
A PHP library to query Google's location service for geolocation and reverse lookups based on a given address, a geo location or a Google Places ID.
高级语言: PHP
项目地址: git://github.com/chroma-x/php-google-geocoder.git
创建时间: 2016-04-22T07:35:25Z
项目社区:https://github.com/chroma-x/php-google-geocoder

开源协议:MIT License

下载


PHP Google Geocoder

Build Status
Test Coverage
Dependency Status
Code Climate
Latest Stable Version
Total Downloads
License

A PHP library to query Google’s location service for geolocation and reverse lookups based on a given address, a geo location or a Google Places ID.

Installation

  1. {
  2. "require": {
  3. "chroma-x/google-geocoder": "~3.0"
  4. }
  5. }

Usage

Autoloading and namesapce

  1. require_once('path/to/vendor/autoload.php');

Performing a GeoLookup

Resolving an address

The API provides an optional usage of an API key to circumvent API quota limits. Please visit the Google API console to receive an API key.

  1. use ChromaX\CommonException;
  2. try{
  3. // Perform lookup
  4. $addressLookup = new ChromaX\GoogleGeocode\Lookup\AddressLookup();
  5. $addressLookup = new Markenwerk\GoogleGeocode\Lookup\AddressLookup();
  6. // Optional adding an API key
  7. $addressLookup->setApiKey('MY_GOOGLE_GEOCODING_API_KEY');
  8. // Submit lookup
  9. $addressLookup->lookup('Germany, 24105 Kiel, Lornsenstraße 43');
  10. // Retrieving the lookup as an array of ChromaX\GoogleGeocode\Result\GeoLookupResult instances
  11. $lookupResults = $addressLookup->getResults();
  12. // Get the number of lookup results
  13. $lookupResultCount = $addressLookup->getResultCount();
  14. // Retrieving the first lookup result as ChromaX\GoogleGeocode\Result\GeoLookupResult instance
  15. $firstResult = $addressLookup->getFirstResult();
  16. } catch (CommonException\NetworkException\CurlException $exception) {
  17. // Google Geocode API is not reachable or curl failed
  18. } catch (CommonException\ApiException\InvalidResponseException $exception) {
  19. // Google Geocode API unexpected result
  20. } catch (CommonException\ApiException\RequestQuotaException $exception) {
  21. // Google Geocode API requests over the allowed limit
  22. } catch (CommonException\ApiException\NoResultException $exception) {
  23. // Google Geocode API request had no result
  24. }

Resolving a geo location

The API provides an optional usage of an API key to circumvent API quota limits. Please visit the Google API console to receive an API key.

  1. use ChromaX\CommonException;
  2. try{
  3. // Perform lookup
  4. $geoLocationLookup = new ChromaX\GoogleGeocode\Lookup\GeoLocationLookup();
  5. $geoLocationLookup = new Markenwerk\GoogleGeocode\Lookup\GeoLocationLookup();
  6. // Optional adding an API key
  7. $geoLocationLookup->setApiKey('MY_GOOGLE_GEOCODING_API_KEY');
  8. // Submit lookup
  9. $geoLocationLookup->lookup(54.334123, 10.1364007);
  10. // Retrieving the lookup as an array of ChromaX\GoogleGeocode\Result\GeoLookupResult instances
  11. $lookupResults = $geoLocationLookup->getResults();
  12. // Get the number of lookup results
  13. $lookupResultCount = $geoLocationLookup->getResultCount();
  14. // Retrieving the first lookup result as ChromaX\GoogleGeocode\Result\AddressLookupResult instance
  15. $firstResult = $geoLocationLookup->getFirstResult();
  16. } catch (CommonException\NetworkException\CurlException $exception) {
  17. // Google Geocode API is not reachable or curl failed
  18. } catch (CommonException\ApiException\InvalidResponseException $exception) {
  19. // Google Geocode API unexpected result
  20. } catch (CommonException\ApiException\RequestQuotaException $exception) {
  21. // Google Geocode API requests over the allowed limit
  22. } catch (CommonException\ApiException\NoResultException $exception) {
  23. // Google Geocode API request had no result
  24. }

Resolving a Google Places ID

Resolving Google Places IDs utilizes the Google Places API. Therefore a Places API key is mandatory for performing a lookup. Please visit the Google API console to receive an API key.

  1. use ChromaX\CommonException;
  2. try{
  3. // Perform lookup
  4. $googlePlacesLookup = new ChromaX\GoogleGeocode\Lookup\GooglePlacesLookup();
  5. $googlePlacesLookup
  6. ->setApiKey('MY_GOOGLE_PLACES_API_KEY')
  7. ->lookup('ChIJ_zNzWmpWskcRP8DWT5eX5jQ');
  8. // Retrieving the lookup as an array of ChromaX\GoogleGeocode\Result\GeoLookupResult instances
  9. $lookupResults = $googlePlacesLookup->getResults();
  10. // Get the number of lookup results
  11. $lookupResultCount = $googlePlacesLookup->getResultCount();
  12. // Retrieving the first lookup result as ChromaX\GoogleGeocode\Result\AddressLookupResult instance
  13. $firstResult = $googlePlacesLookup->getFirstResult();
  14. } catch (CommonException\NetworkException\CurlException $exception) {
  15. // Google Geocode API is not reachable or curl failed
  16. } catch (CommonException\ApiException\InvalidResponseException $exception) {
  17. // Google Geocode API unexpected result
  18. } catch (CommonException\ApiException\RequestQuotaException $exception) {
  19. // Google Geocode API requests over the allowed limit
  20. } catch (CommonException\ApiException\AuthenticationException $exception) {
  21. // Google Places service API key invalid
  22. } catch (CommonException\ApiException\NoResultException $exception) {
  23. // Google Geocode API request had no result
  24. }

Reading from a GeoLookupResult

Attention: Plaese note that all getter methods on the GeoLocationAddress return a GeoLocationAddressComponent instance or null. For preventing calls on non-objects the GeoLocationAddress class provides methods to check whether the address components exists.

  1. // Retrieving the first lookup result as ChromaX\GoogleGeocode\Result\GeoLookupResult instance
  2. $firstResult = $addressLookup->getFirstResult();
  3. // Retieving address information as ChromaX\GoogleGeocode\GeoLocation\GeoLocationAddress
  4. $geoLocationAddress = $firstResult->getAddress();
  5. if($firstResult->hasAddress()) {
  6. // Retrieving the address information from the lookup result
  7. if($firstResult->getAddress()->hasStreetName()) {
  8. // Returns 'Lornsenstraße'
  9. $addressStreetShort = $firstResult->getAddress()->getStreetName()->getShortName();
  10. // Returns 'Lornsenstraße'
  11. $addressStreetLong = $firstResult->getAddress()->getStreetName()->getLongName();
  12. }
  13. if($firstResult->getAddress()->hasStreetNumber()) {
  14. // Returns '43'
  15. $addressStreetNumberShort = $firstResult->getAddress()->getStreetNumber()->getShortName();
  16. // Returns '43'
  17. $addressStreetNumberLong = $firstResult->getAddress()->getStreetNumber()->getLongName();
  18. }
  19. if($firstResult->getAddress()->hasPostalCode()) {
  20. // Returns '24105'
  21. $addressPostalCodeShort = $firstResult->getAddress()->getPostalCode()->getShortName();
  22. // Returns '24105'
  23. $addressPostalCodeLong = $firstResult->getAddress()->getPostalCode()->getLongName();
  24. }
  25. if($firstResult->getAddress()->hasCity()) {
  26. // Returns 'KI'
  27. $addressCityShort = $firstResult->getAddress()->getCity()->getShortName();
  28. // Returns 'Kiel'
  29. $addressCityLong = $firstResult->getAddress()->getCity()->getLongName();
  30. }
  31. if($firstResult->getAddress()->hasArea()) {
  32. // Returns 'Ravensberg - Brunswik - Düsternbrook'
  33. $addressAreaShort = $firstResult->getAddress()->getArea()->getShortName();
  34. // Returns 'Ravensberg - Brunswik - Düsternbrook'
  35. $addressAreaLong = $firstResult->getAddress()->getArea()->getLongName();
  36. }
  37. if($firstResult->getAddress()->hasProvince()) {
  38. // Returns 'SH'
  39. $addressProvinceShort = $firstResult->getAddress()->getProvince()->getShortName();
  40. // Returns 'Schleswig-Holstein'
  41. $addressProvinceLong = $firstResult->getAddress()->getProvince()->getLongName();
  42. }
  43. if($firstResult->getAddress()->hasCountry()) {
  44. // Returns 'DE'
  45. $addressCountryShort = $firstResult->getAddress()->getCountry()->getShortName();
  46. // Returns 'Germany'
  47. $addressCountryLong = $firstResult->getAddress()->getCountry()->getLongName();
  48. }
  49. }
  50. if($firstResult->hasGeometry()) {
  51. // Retrieving the geometry information from the lookup result
  52. if($firstResult->getGeometry()->hasLocation()) {
  53. // Returns 54.334123
  54. $geometryLocationLatitude = $firstResult->getGeometry()->getLocation()->getLatitude();
  55. // Returns 10.1364007
  56. $geometryLocationLatitude = $firstResult->getGeometry()->getLocation()->getLongitude();
  57. }
  58. if($firstResult->getGeometry()->hasViewport()) {
  59. // Returns 54.335471980291
  60. $geometryLocationLatitude = $firstResult->getGeometry()->getViewport()->getNortheast()->getLatitude();
  61. // Returns 10.137749680292
  62. $geometryLocationLatitude = $firstResult->getGeometry()->getViewport()->getNortheast()->getLongitude();
  63. // Returns 54.332774019708
  64. $geometryLocationLatitude = $firstResult->getGeometry()->getViewport()->getSouthwest()->getLatitude();
  65. // Returns 10.135051719708
  66. $geometryLocationLatitude = $firstResult->getGeometry()->getViewport()->getSouthwest()->getLongitude();
  67. }
  68. }
  69. if($firstResult->hasGooglePlacesId()) {
  70. // Retrieving the Google Places information from the lookup result
  71. // Returns 'ChIJ_zNzWmpWskcRP8DWT5eX5jQ'
  72. $googlePlacesId = $firstResult->getGooglePlacesId();
  73. }

Exception handling

PHP Google Geocoder provides different exceptions provided by the PHP Common Exceptions project for proper handling.
You can find more information about PHP Common Exceptions at Github.

Contribution

Contributing to our projects is always very appreciated.
But: please follow the contribution guidelines written down in the CONTRIBUTING.md document.

License

PHP Google Geocoder is under the MIT license.