lightools/geocoding

此包已被弃用且不再维护。未建议替代包。

仅提供精确结果的地址地理编码 - 可用于地址验证。

dev-master 2017-02-05 12:05 UTC

This package is auto-updated.

Last update: 2022-10-04 10:17:03 UTC


README

仅提供精确结果的简单地址地理编码 - 可用于完整的地址验证。例如地址 Revoluční 11, Praha 将被找到,但 Revoluční, Praha 则不会。

安装

$ composer require lightools/geocoding

简单用法

try {
    $httpClient = new Bitbang\Http\Clients\CurlClient();
    $geocoder = new Lightools\Geocoding\GoogleGeocoder($httpClient);
    $geocoded = $geocoder->geocode('Revoluční 11, Praha');

    echo $geocoded->getLatitude();
    echo $geocoded->getLongitude();
    echo $geocoded->getPostalCode();

} catch (Lightools\Geocoding\GeocodingFailedException $e) {
    // e.g. HTTP request failed
} catch (Lightools\Geocoding\NoExactResultException $e) {
    // invalid or inaccurate address
} catch (Lightools\Geocoding\QuotaLimitException $e) {
    // rate limit exceeded
}

链式调用、缓存、配置

此库包含简单的缓存地理编码器和链式地理编码器。您可以通过构造函数中的第二个参数配置链式地理编码器,指定异常情况下的跳过条件。如果您想创建自己的,只需实现接口 IGeocoder

对于 Google 地理编码器,您可以配置任何所需的查询参数,只需调用 setParameters 并设置例如语言或组件。Smartform 地理编码器有相同的方法,但配置项不多 - 只有目标国家。

$httpClient = new Bitbang\Http\Clients\CurlClient();

$googleGeocoder = new Lightools\Geocoding\GoogleGeocoder($httpClient);
$googleGeocoder->setParameters(['components' => 'country:CZ']);

$smartformGeocoder = new Lightools\Geocoding\SmartformGeocoder('password', $httpClient);
$smartformGeocoder->setParameters(['countries' => ['CZ']]);

$chainGeocoder = new Lightools\Geocoding\ChainGeocoder([$smartformGeocoder, $googleGeocoder]);
$cachedGeocoder = new Lightools\Geocoding\CachedGeocoder($chainGeocoder, __DIR__ . '/cache/geocoding');

$cachedGeocoder->geocode('Václavské náměstí 837/11, Praha');

如何运行测试

$ vendor/bin/tester tests