powderblue / geocoding-api
用于操作Google Geocoding API的基本PHP客户端
v4.0.0
2024-04-16 12:45 UTC
Requires
- php: ^7.4|^8.1.3
- ext-curl: *
Requires (Dev)
- danbettles/codesniffer-standard: ^2.0.0
- phpstan/phpstan: ^1.10.67
- phpunit/phpunit: ^9.6.19
- squizlabs/php_codesniffer: ^3.9.1
README
一个基本的PHP客户端,用于与Google的Geocoding API交互。
注意
仅需要PHP7+和cURL扩展:在生产环境中没有其他依赖项
演示
如果您想查看库的实际效果,可以执行tests/geocode.php
,该文件对各种Geocode
方法运行一系列测试。
重要
在运行tests/geocode.php
之前,您必须将项目的API密钥添加到tests/.config.php
返回值
地理编码方法,包括Geocode::byAddress()
和Geocode::byPostcode()
,在成功时返回数组,否则抛出异常。数组的结构基于Schema.org GeoCoordinates类型,如下所示。
Array ( [address] => Array ( [streetAddress] => 25 Old Gardens Close [addressLocality] => Tunbridge Wells [addressRegion] => Kent [postalCode] => TN2 5ND [addressCountry] => GB ) [latitude] => 51.1172303 [longitude] => 0.2635245 )
正向地理编码
使用便利方法对地址进行地理编码的示例
$geocode = new Geocode('<your-api-key>'); // Without using region biasing $geoCoordinates = $geocode->byAddress('25 Old Gardens Close Tunbridge Wells TN2 5ND'); // `byAddress()` accepts an *ISO 3166-1 alpha-2 code* to apply bias by a specific country $geoCoordinates = $geocode->byAddress('25 Old Gardens Close Tunbridge Wells TN2 5ND', 'GB');
使用便利方法对邮编进行地理编码的示例
$geocode = new Geocode('<your-api-key>'); // `byPostcode()` *requires* a country; this is to help reduce ambiguity and, therefore, improve results $geoCoordinates = $geocode->byPostcode('07001', 'ES'); // => Palma, Majorca
反向地理编码
使用便利方法进行反向地理编码的示例
$geocode = new Geocode('<your-api-key>'); $geoCoordinates = $geocode->byLatLong('50.88916732998306', '-0.5768395884825535'); // => Arundel, West Sussex, GB