geocoder-php / google-maps-places-provider
Google Maps Places 地理编码适配器
1.4.1
2023-07-09 14:05 UTC
Requires
- php: ^7.4 || ^8.0
- geocoder-php/common-http: ^4.0
- willdurand/geocoder: ^4.0
Requires (Dev)
- geocoder-php/provider-integration-tests: ^1.0
- php-http/curl-client: ^2.2
- php-http/message: ^1.0
- phpunit/phpunit: ^9.5
Provides
README
这是 PHP Geocoder 的 Google Places 提供程序。这是一个 只读 仓库。有关信息和文档,请参阅 主仓库。
安装
composer require geocoder-php/google-maps-places-provider
API 文档
https://developers.google.com/places/web-service
用法
由于底层 Places API 的要求,此提供程序在执行查询时通常需要额外的数据。
地理编码
此提供程序支持两种不同的文本地理编码模式。
查找模式
这是默认模式。它需要一个精确的地点名称。它不是很宽容,通常只返回一个结果
$results = $provider->geocodeQuery( GeocodeQuery::create('Museum of Contemporary Art Australia') );
搜索模式
此模式将根据输入文本执行搜索。它比 find
模式更宽容,但结果将包含所有字段,因此将以最高费率计费。
$results = $provider->geocodeQuery( GeocodeQuery::create('art museum sydney') ->withData('mode', GoogleMapsPlaces::GEOCODE_MODE_SEARCH) );
周围位置(类似于反向地理编码,见下文)
$results = $provider->geocodeQuery( GeocodeQuery::create('bar') ->withData('mode', GoogleMapsPlaces::GEOCODE_MODE_SEARCH) ->withData('location', '-32.926642, 151.783026') );
国家与国家名称或两位字母 ISO 3166-1 国家代码匹配。如果您仅使用 "region" 参数,则不能保证有区域结果,因为文档指出 区域
region 参数将仅影响,而不是完全限制地理编码的结果。
$results = $provider->geocodeQuery( GeocodeQuery::create('montpellier') ->withData('components', 'country:FR'); );
反向地理编码
latlon 坐标反向地理编码有三个选项
- 模式
search
+ 类型(例如)bar
:使用 Google Place API 文本搜索,需要type
- 类似于:搜索周围位置(见上一节)
- 模式
nearby
+ rankbydistance
:使用 Google Place API 附近搜索,需要type/keyword/name
- 模式
nearby
+ rankbyprominence
:使用 Google Place API 附近搜索,需要radius
默认模式:search
(因为向后兼容性)。当使用模式 nearby
时,默认 rankby 为 prominence
。模式 search
+ type 和模式 nearby
+ type/keyword/name 非常相似。模式 search
提供格式化的地址,模式 nearby
提供vicinity。例如。
search
:有 "formatted_address":"7 Cope St, Redfern NSW 2016"nearby
:有 "vicinity" 而不是:"7 Cope St, Redfern"
示例
$results = $provider->reverseQuery( ReverseQuery::fromCoordinates(-33.892674, 151.200727) // ->withData('mode', GoogleMapsPlaces::GEOCODE_MODE_SEARCH) // =default ->withData('type', 'bar') // requires type ); $address = $results->first()->getFormattedAddress();
$results = $provider->reverseQuery( ReverseQuery::fromCoordinates(-33.892674, 151.200727) ->withData('mode', GoogleMapsPlaces::GEOCODE_MODE_NEARBY) //->withData('rankby','prominence'); // =default ->withData('radius', 500) // requires radius (meters) ); $vicinity = $results->first()->getVicinity();
$results = $provider->reverseQuery( ReverseQuery::fromCoordinates(-33.892674, 151.200727) ->withData('mode', GoogleMapsPlaces::GEOCODE_MODE_NEARBY) ->withData('rankby','distance'); ->withData('keyword', 'bar') // requires type/keyword/name );