chroma-x/google-geocoder

一个 PHP 库,用于查询 Google 的位置服务,基于给定的地址、地理位置或 Google 地点 ID 进行地理位置和反向查找。

3.0.5 2021-01-18 16:09 UTC

This package is auto-updated.

Last update: 2024-09-19 00:43:14 UTC


README

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

一个 PHP 库,用于查询 Google 的位置服务,基于给定的地址、地理位置或 Google 地点 ID 进行地理位置和反向查找。

安装

{
   	"require": {
        "chroma-x/google-geocoder": "~3.0"
    }
}

使用

自动加载和命名空间

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

执行地理查找

解析地址

API 提供了可选的 API 密钥使用,以绕过 API 配额限制。请访问 Google 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 配额限制。请访问 Google 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
}

解析 Google 地点 ID

解析 Google 地点 ID 使用 Google 地点 API。因此,执行查找必须要有地点 API 密钥。请访问 Google 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 的获取方法都返回一个 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 项目提供的不同异常,以便正确处理。
您可以在 Github 上的 PHP Common Exceptions 获取更多信息

贡献

对项目的贡献总是非常受欢迎。
但:请遵循在 CONTRIBUTING.md 文档中记录的贡献指南。

许可证

PHP Google Geocoder 在 MIT 许可证下。