phpfacile/geocoding

服务主要针对获取位置(地址、地点)的坐标(纬度、经度)。

1.0.2 2018-11-13 18:11 UTC

This package is auto-updated.

Last update: 2024-09-14 07:09:55 UTC


README

该服务主要是用于获取位置(地址、地点)的坐标(纬度、经度)。

它基于 geocoder-php (https://github.com/geocoder-php/Geocoder),并尝试使用符合我们要求的提供商

  • 提供商必须允许存储地理编码输出
  • 提供商的使用必须是免费的(对于少量查询)

注意:使用缓存是为了尽可能满足提供商定义的使用限制

安装

在项目的根目录下执行

composer require phpfacile/geocoding

或者在 composer.json 文件的 "require" 部分添加 "phpfacile/geocoding": "^1.0"

"require": {
    "phpfacile/geocoding": "^1.0"
}

提供商比较

使用方法

$geocodingService = new GeocodingService($cfg, $cacheDir);

$cfg = [
    'geonames' => [
        'username' => '<replace with geonames username>',
    ],
    'nominatim' => [
        'rootUrl' => 'https://nominatim.openstreetmap.org',
        'userAgent' => '<replace with a user-agent>',
    ]
];

$cacheDir.'/nominatim' 必须存在(例如:如果 $cacheDir='/cache',则 '/cache/nominatim' 文件夹必须存在并且可写)

实际上,在当前的实现中,任何情况下 nominatim 都是首选提供商。

$locations = $this->geocodingService->getLocationsByAddress('Etretat, France');
foreach ($locations as $location) {
    var_dump($location);
}

$locations = $this->geocodingService->getPlacesByCountryAndPlaceName('France', 'Etretat');
foreach ($locations as $location) {
    var_dump($location);
}

免责声明

Nominatim 可能随时停止其服务

故障排除

如果您修改了代码以使用 geonames,您需要知道 geonames 提供商(https://github.com/geocoder-php/geonames-provider)会静默忽略以下错误消息

{"status":{"message":"user account not enabled to use the free webservice. Please enable it on your account page: http://www.geonames.org/manageaccount ","value":10}}

待办事项

  • 管理定位
  • 移除意外的结果(例如,使用 nominatim,对 "Paris, France" 的查询目前返回一个类型为 "city" 的条目,以及一个几乎相同的另一个条目,但类型为 "administrative")