markenwerk / google-places-suite
该软件包已被废弃,不再维护。未建议替代软件包。
一个PHP库,用于查询谷歌的地点服务,查询地点和地址,并通过地点ID获取详细信息。
3.0.1
2016-07-15 15:19 UTC
Requires
- php: >=5.3
- lib-curl: *
- markenwerk/common-exceptions: ~3.0
- markenwerk/google-datastructures: ~2.0
Requires (Dev)
- codeclimate/php-test-reporter: dev-master
- phpunit/phpunit: ~4.8
Suggests
- markenwerk/google-geocoder: 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.
README
一个PHP库,用于查询谷歌的地点服务,查询地点和地址,并通过地点ID获取详细信息。
安装
{
"require": {
"markenwerk/google-places-suite": "~3.0"
}
}
使用方法
自动加载和命名空间
require_once('path/to/vendor/autoload.php');
执行谷歌地点查询
获取已知谷歌地点ID的详细信息
use Markenwerk\CommonException;
try{
// Perform query
$googlePlacesDetailQuery = new GooglePlacesDetailQuery();
$googlePlacesDetailQuery
->setApiKey($this->googlePlacesApiKey)
->query('GOOGLE_PLACES_ID');
// Retrieving the query result as Markenwerk\GooglePlacesSuite\GooglePlacesDetailResult instance
$queryResult = $googlePlacesDetailQuery->getResult();
} catch (CommonException\NetworkException\CurlException) {
// Google Places service is not reachable or curl failed
} catch (CommonException\ApiException\InvalidResponseException $exception) {
// Google Places service invalid response
} catch (CommonException\ApiException\RequestQuotaException $exception) {
// Google Places service requests over the allowed limit
} catch (Markenwerk\CommonException\ApiException\AuthenticationException $exception) {
// Google Places service API key invalid
} catch (CommonException\ApiException\NoResultException $exception) {
// Google places service query had no result
}
从GooglePlacesDetailResult读取
注意: 请注意,所有 GeoLocationAddress 上的获取方法都返回一个 GeoLocationAddressComponent 实例或 null。为了防止对非对象的调用,GeoLocationAddress 类提供了一些方法来检查地址组件是否存在。
// Retrieving the query result as Markenwerk\GooglePlacesSuite\GooglePlacesDetailResult instance
$queryResult = $googlePlacesDetailQuery->getResult();
// Retieving address information as Markenwerk\GoogleDataStructure\GeoLocation\GeoLocationAddress
if($queryResult->hasAddress()) {
if ($queryResult->getAddress()->hasStreetName()) {
// Returns 'Lornsenstraße'
$addressStreetShort = $queryResult->getAddress()->getStreetName()->getShortName();
// Returns 'Lornsenstraße'
$addressStreetLong = $queryResult->getAddress()->getStreetName()->getLongName();
}
if ($queryResult->getAddress()->hasStreetNumber()) {
// Returns '43'
$addressStreetNumberShort = $queryResult->getAddress()->getStreetNumber()->getShortName();
// Returns '43'
$addressStreetNumberLong = $queryResult->getAddress()->getStreetNumber()->getLongName();
}
if ($queryResult->getAddress()->hasPostalCode()) {
// Returns '24105'
$addressPostalCodeShort = $queryResult->getAddress()->getPostalCode()->getShortName();
// Returns '24105'
$addressPostalCodeLong = $queryResult->getAddress()->getPostalCode()->getLongName();
}
if ($queryResult->getAddress()->hasCity()) {
// Returns 'KI'
$addressCityShort = $queryResult->getAddress()->getCity()->getShortName();
// Returns 'Kiel'
$addressCityLong = $queryResult->getAddress()->getCity()->getLongName();
}
if ($queryResult->getAddress()->hasArea()) {
// Returns 'Ravensberg - Brunswik - Düsternbrook'
$addressAreaShort = $queryResult->getAddress()->getArea()->getShortName();
// Returns 'Ravensberg - Brunswik - Düsternbrook'
$addressAreaLong = $queryResult->getAddress()->getArea()->getLongName();
}
if ($queryResult->getAddress()->hasProvince()) {
// Returns 'SH'
$addressProvinceShort = $queryResult->getAddress()->getProvince()->getShortName();
// Returns 'Schleswig-Holstein'
$addressProvinceLong = $queryResult->getAddress()->getProvince()->getLongName();
}
if ($queryResult->getAddress()->hasCountry()) {
// Returns 'DE'
$addressCountryShort = $queryResult->getAddress()->getCountry()->getShortName();
// Returns 'Germany'
$addressCountryLong = $queryResult->getAddress()->getCountry()->getLongName();
}
}
// Retieving address information as Markenwerk\GoogleDataStructure\GeoLocation\GeoLocationGeometry
if ($queryResult->hasGeometry()) {
if ($queryResult->getGeometry()->hasLocation()) {
// Returns 54.334123
$geometryLocationLatitude = $queryResult->getGeometry()->getLocation()->getLatitude();
// Returns 10.1364007
$geometryLocationLatitude = $queryResult->getGeometry()->getLocation()->getLongitude();
}
if ($queryResult->getGeometry()->hasViewport()) {
// Returns 54.335471980291
$geometryLocationLatitude = $queryResult->getGeometry()->getViewport()->getNortheast()->getLatitude();
// Returns 10.137749680292
$geometryLocationLatitude = $queryResult->getGeometry()->getViewport()->getNortheast()->getLongitude();
// Returns 54.332774019708
$geometryLocationLatitude = $queryResult->getGeometry()->getViewport()->getSouthwest()->getLatitude();
// Returns 10.135051719708
$geometryLocationLatitude = $queryResult->getGeometry()->getViewport()->getSouthwest()->getLongitude();
}
if ($queryResult->getGeometry()->hasAccessPoints()) {
for ($i = 0; $i < $queryResult->getGeometry()->countAccessPoints(); $i++) {
// Returns 54.335471980291
$geometryAccessPointLatitude = $queryResult->getGeometry()->getAccessPointAt($i)->getLatitude();
// Returns 10.137749680292
$geometryAccessPointLatitude = $queryResult->getGeometry()->getAccessPointAt($i)->getLongitude();
}
}
}
if ($queryResult->hasGooglePlacesId()) {
// Retrieving the Google Places information from the query result
// Returns 'ChIJ_zNzWmpWskcRP8DWT5eX5jQ'
$googlePlacesId = $queryResult->getGooglePlacesId();
}
异常处理
PHP Google Places Suite 提供了由 PHP Common Exceptions 项目提供的不同异常,以便进行适当的处理。
您可以在GitHub上的PHP Common Exceptions找到更多信息。
贡献
对我们项目的贡献总是非常受欢迎。
但是:请按照CONTRIBUTING.md 文档中写下的贡献指南操作。
许可
PHP Google Places Suite 在MIT许可下。