markenwerk / google-geocoder
3.0.5
2021-01-18 16:09 UTC
Requires
- php: >=5.3
- ext-curl: *
- ext-json: *
- lib-curl: *
- chroma-x/common-exceptions: ~3.0
- chroma-x/google-datastructures: ~2.0
Requires (Dev)
- codeclimate/php-test-reporter: dev-master
- phpunit/phpunit: ~4.8
README
一个PHP库,用于查询谷歌位置服务以进行地理定位和基于给定地址、地理位置或谷歌地点ID的反向查找。
安装
{
"require": {
"chroma-x/google-geocoder": "~3.0"
}
}
使用方法
自动加载和命名空间
require_once('path/to/vendor/autoload.php');
执行地理查找
解析地址
API提供可选的API密钥使用,以绕过API配额限制。请访问谷歌API控制台获取API密钥。
use ChromaX\CommonException;
try{
// Perform lookup
$addressLookup = new ChromaX\GoogleGeocode\Lookup\AddressLookup();
$addressLookup = new Markenwerk\GoogleGeocode\Lookup\AddressLookup();
// Optional adding an API key
$addressLookup->setApiKey('MY_GOOGLE_GEOCODING_API_KEY');
// Submit lookup
$addressLookup->lookup('Germany, 24105 Kiel, Lornsenstraße 43');
// Retrieving the lookup as an array of ChromaX\GoogleGeocode\Result\GeoLookupResult instances
$lookupResults = $addressLookup->getResults();
// Get the number of lookup results
$lookupResultCount = $addressLookup->getResultCount();
// Retrieving the first lookup result as ChromaX\GoogleGeocode\Result\GeoLookupResult instance
$firstResult = $addressLookup->getFirstResult();
} catch (CommonException\NetworkException\CurlException $exception) {
// Google Geocode API is not reachable or curl failed
} catch (CommonException\ApiException\InvalidResponseException $exception) {
// Google Geocode API unexpected result
} catch (CommonException\ApiException\RequestQuotaException $exception) {
// Google Geocode API requests over the allowed limit
} catch (CommonException\ApiException\NoResultException $exception) {
// Google Geocode API request had no result
}
解析地理位置
API提供可选的API密钥使用,以绕过API配额限制。请访问谷歌API控制台获取API密钥。
use ChromaX\CommonException;
try{
// Perform lookup
$geoLocationLookup = new ChromaX\GoogleGeocode\Lookup\GeoLocationLookup();
$geoLocationLookup = new Markenwerk\GoogleGeocode\Lookup\GeoLocationLookup();
// Optional adding an API key
$geoLocationLookup->setApiKey('MY_GOOGLE_GEOCODING_API_KEY');
// Submit lookup
$geoLocationLookup->lookup(54.334123, 10.1364007);
// Retrieving the lookup as an array of ChromaX\GoogleGeocode\Result\GeoLookupResult instances
$lookupResults = $geoLocationLookup->getResults();
// Get the number of lookup results
$lookupResultCount = $geoLocationLookup->getResultCount();
// Retrieving the first lookup result as ChromaX\GoogleGeocode\Result\AddressLookupResult instance
$firstResult = $geoLocationLookup->getFirstResult();
} catch (CommonException\NetworkException\CurlException $exception) {
// Google Geocode API is not reachable or curl failed
} catch (CommonException\ApiException\InvalidResponseException $exception) {
// Google Geocode API unexpected result
} catch (CommonException\ApiException\RequestQuotaException $exception) {
// Google Geocode API requests over the allowed limit
} catch (CommonException\ApiException\NoResultException $exception) {
// Google Geocode API request had no result
}
解析谷歌地点ID
解析谷歌地点ID需要使用谷歌地点API。因此,执行查找必须有一个地点API密钥。请访问谷歌API控制台获取API密钥。
use ChromaX\CommonException;
try{
// Perform lookup
$googlePlacesLookup = new ChromaX\GoogleGeocode\Lookup\GooglePlacesLookup();
$googlePlacesLookup
->setApiKey('MY_GOOGLE_PLACES_API_KEY')
->lookup('ChIJ_zNzWmpWskcRP8DWT5eX5jQ');
// Retrieving the lookup as an array of ChromaX\GoogleGeocode\Result\GeoLookupResult instances
$lookupResults = $googlePlacesLookup->getResults();
// Get the number of lookup results
$lookupResultCount = $googlePlacesLookup->getResultCount();
// Retrieving the first lookup result as ChromaX\GoogleGeocode\Result\AddressLookupResult instance
$firstResult = $googlePlacesLookup->getFirstResult();
} catch (CommonException\NetworkException\CurlException $exception) {
// Google Geocode API is not reachable or curl failed
} catch (CommonException\ApiException\InvalidResponseException $exception) {
// Google Geocode API unexpected result
} catch (CommonException\ApiException\RequestQuotaException $exception) {
// Google Geocode API requests over the allowed limit
} catch (CommonException\ApiException\AuthenticationException $exception) {
// Google Places service API key invalid
} catch (CommonException\ApiException\NoResultException $exception) {
// Google Geocode API request had no result
}
从GeoLookupResult读取
注意:请记住,在GeoLocationAddress上的所有getter方法都返回一个GeoLocationAddressComponent实例或null。为了防止对非对象进行调用,GeoLocationAddress类提供了方法来检查地址组件是否存在。
// Retrieving the first lookup result as ChromaX\GoogleGeocode\Result\GeoLookupResult instance
$firstResult = $addressLookup->getFirstResult();
// Retieving address information as ChromaX\GoogleGeocode\GeoLocation\GeoLocationAddress
$geoLocationAddress = $firstResult->getAddress();
if($firstResult->hasAddress()) {
// Retrieving the address information from the lookup result
if($firstResult->getAddress()->hasStreetName()) {
// Returns 'Lornsenstraße'
$addressStreetShort = $firstResult->getAddress()->getStreetName()->getShortName();
// Returns 'Lornsenstraße'
$addressStreetLong = $firstResult->getAddress()->getStreetName()->getLongName();
}
if($firstResult->getAddress()->hasStreetNumber()) {
// Returns '43'
$addressStreetNumberShort = $firstResult->getAddress()->getStreetNumber()->getShortName();
// Returns '43'
$addressStreetNumberLong = $firstResult->getAddress()->getStreetNumber()->getLongName();
}
if($firstResult->getAddress()->hasPostalCode()) {
// Returns '24105'
$addressPostalCodeShort = $firstResult->getAddress()->getPostalCode()->getShortName();
// Returns '24105'
$addressPostalCodeLong = $firstResult->getAddress()->getPostalCode()->getLongName();
}
if($firstResult->getAddress()->hasCity()) {
// Returns 'KI'
$addressCityShort = $firstResult->getAddress()->getCity()->getShortName();
// Returns 'Kiel'
$addressCityLong = $firstResult->getAddress()->getCity()->getLongName();
}
if($firstResult->getAddress()->hasArea()) {
// Returns 'Ravensberg - Brunswik - Düsternbrook'
$addressAreaShort = $firstResult->getAddress()->getArea()->getShortName();
// Returns 'Ravensberg - Brunswik - Düsternbrook'
$addressAreaLong = $firstResult->getAddress()->getArea()->getLongName();
}
if($firstResult->getAddress()->hasProvince()) {
// Returns 'SH'
$addressProvinceShort = $firstResult->getAddress()->getProvince()->getShortName();
// Returns 'Schleswig-Holstein'
$addressProvinceLong = $firstResult->getAddress()->getProvince()->getLongName();
}
if($firstResult->getAddress()->hasCountry()) {
// Returns 'DE'
$addressCountryShort = $firstResult->getAddress()->getCountry()->getShortName();
// Returns 'Germany'
$addressCountryLong = $firstResult->getAddress()->getCountry()->getLongName();
}
}
if($firstResult->hasGeometry()) {
// Retrieving the geometry information from the lookup result
if($firstResult->getGeometry()->hasLocation()) {
// Returns 54.334123
$geometryLocationLatitude = $firstResult->getGeometry()->getLocation()->getLatitude();
// Returns 10.1364007
$geometryLocationLatitude = $firstResult->getGeometry()->getLocation()->getLongitude();
}
if($firstResult->getGeometry()->hasViewport()) {
// Returns 54.335471980291
$geometryLocationLatitude = $firstResult->getGeometry()->getViewport()->getNortheast()->getLatitude();
// Returns 10.137749680292
$geometryLocationLatitude = $firstResult->getGeometry()->getViewport()->getNortheast()->getLongitude();
// Returns 54.332774019708
$geometryLocationLatitude = $firstResult->getGeometry()->getViewport()->getSouthwest()->getLatitude();
// Returns 10.135051719708
$geometryLocationLatitude = $firstResult->getGeometry()->getViewport()->getSouthwest()->getLongitude();
}
}
if($firstResult->hasGooglePlacesId()) {
// Retrieving the Google Places information from the lookup result
// Returns 'ChIJ_zNzWmpWskcRP8DWT5eX5jQ'
$googlePlacesId = $firstResult->getGooglePlacesId();
}
异常处理
PHP Google Geocoder提供了PHP Common Exceptions项目提供的不同异常,以便正确处理。
有关PHP Common Exceptions的更多信息,请访问Github。
贡献
我们非常欢迎为我们的项目做出贡献。
但是:请遵循CONTRIBUTING.md文档中写下的贡献指南。
许可证
PHP Google Geocoder采用MIT许可证。