nr / fieldtypegeocoder
从多个提供商收集和存储地理编码信息
1.0.5
2023-06-20 11:32 UTC
Requires
- ext-curl: *
- ext-json: *
- geocoder-php/open-cage-provider: ^4.6
- hari/pw-module: ~1.0
- nyholm/psr7: ^1.8
- php-http/curl-client: ^2.2
README
它做什么
从外部地理编码服务检索、收集和存储地理位置数据。底层,该模块使用 William Durand 和 Tobias Nyholm 的优秀 PHP 库 geocoder-php,并添加了一些 processwire 魔法。感谢 Ryan(FieldtypeMapMarker)和 mats(FieldtypeLeafletMapMarker),我们从他们那里获得了许多灵感来开发此模块!
特性
- 在格式化的地址中进行全文搜索
- 邻近度搜索
- 在 geojson 中搜索
- 易于挂钩的地理编码提供商(支持的提供商)
- 来自 geocoder-php 的标准化地理编码对象
- 支持 dadish 的 GraphQL-Module
安装
- 将此模块的文件复制到 /site/modules/FieldtypeGeocoder/
- 在 /site/modules/FieldtypeGeocoder/ 目录中执行以下命令。
composer install # if you get an php version error use /usr/bin/php7.4 /usr/local/bin/composer update
- 在 processwire 管理员中:模块 > 刷新和安装 > 地理编码。
- 插入您地理编码提供商的 API 密钥。默认提供商是 OpenCage。OpenCage 使用各种其他地理编码服务。您可以使用 processwire 钩子更改提供商。了解更多
- 创建一个新的地理编码类型字段,并命名为您喜欢的名称。在我们的示例中,我们简单地将其命名为 "geocoder"。
- 将字段添加到模板中,并开始地理编码!
- 对于 GraphQL,安装
GraphQLFieldtypeGeocoder
模块
通过 composer 安装
- 在您的网站根目录中执行以下命令。
composer require nr/fieldtypegeocoder
要求
- PHP >= 7.4
- PHP 扩展:json、curl、intl
模块配置
模块
> 配置
> FieldtypeGeocoder
在此处插入您的 API 密钥。默认
字段配置
字段
> your_field
> input
每个字段都可以有默认地图中心
API 参考
在输入中搜索
// Fulltextsearch $pages->find('geocoder*=Berl');
在格式化的地址中搜索
// Fulltextsearch $pages->find('geocoder.formatted*=Berlin');
在属性中搜索
// Simplesearch $pages->find('geocoder.properties.timezone=Europe/Berlin'); $pages->find('geocoder.properties.locality=Berlin'); $pages->find('geocoder.properties.lat>10');
按邻近度搜索和排序
$pages->find('geocoder.proximity=52.473758|13.402580, limit=3');
按状态搜索
/** * @see Geocoder::statusOn * @see Geocoder::statusSingleResult * @see Geocoder::statusMultipleResults */ $pages->find('geocoder.status=3'); // Status "On" and "SingleResult" $pages->find('geocoder.status&2|4'); // Status "SingleResult" or "MultipleResults"
地理编码对象
ProcessWire\Geocoder Object ( [data] => Array ( [status] => 5 [formatted] => Berlin, Deutschland [query] => Berlin [geodata] => Array ( [type] => Feature [bounds] => Array ( [east] => 13,5488599 [west] => 13,2288599 [north] => 52,6770365 [south] => 52,3570365 ) [geometry] => Array ( [type] => Point [coordinates] => Array ( [0] => 13,3888599 [1] => 52,5170365 ) ) [properties] => Array ( [country] => Deutschland [locality] => Berlin [timezone] => Europe/Berlin [postalCode] => 10117 [providedBy] => opencage [adminLevels] => Array ( [1] => Array ( [code] => BE [name] => Berlin [level] => 1 ) ) [countryCode] => DE ) ) [lat] => 52,517036 [lng] => 13,38886 [provider] => opencage ) )
钩子/更改提供商
您可以将某些方法挂钩以更改或覆盖地理编码提供商。在这里您可以找到支持的所有提供商的完整列表。此处
- 下载、解压提供商包。
- 移动您的文件夹结构中的文件(
Provider.php
和ProviderAddress.php
)。 - 使用
require_once()
命令加载所有文件。
*将 "Provider" 替换为提供商名称,例如 Google 或 Mapbox 等。
示例 1:Google Maps 提供商包
- 地理编码 API 文档和示例
- 地图平台 配置您的 API 密钥
/** @global Wire $wire */ $wire->addHookBefore('FieldtypeGeocoder::getProvider', function(HookEvent $event) { require_once (/*NoCompile*/__DIR__ .'/providers/GoogleMaps.php'); require_once (/*NoCompile*/__DIR__ .'/providers/GoogleAddress.php'); $fieldtype = $event->object; $apiKey = $fieldtype->apiKey; // or insert the key direct $adapter = $event->argumentsByName('adapter'); $event->return = new \Geocoder\Provider\GoogleMaps\GoogleMaps($adapter, null, $apiKey); $event->replace = true; });
示例 2:Mapbox 搜索提供商包
- 地理编码 API 文档和示例
- Playground Playground(👍)
- 访问令牌 创建和管理您的密钥
use Geocoder\Provider\Mapbox\Mapbox; use Geocoder\Query\GeocodeQuery; use Geocoder\Query\ReverseQuery; /** @global Wire $wire */ $wire->addHookBefore('FieldtypeGeocoder::getProvider', function(HookEvent $event) { require_once (/*NoCompile*/__DIR__ .'/providers/Mapbox.php'); require_once (/*NoCompile*/__DIR__ .'/providers/MapboxAddress.php'); $fieldtype = $event->object; $adapter = $event->argumentsByName('adapter'); $event->return = new Mapbox($adapter, $fieldtype->apiKey, null); $event->replace = true; }); /** * Manipulate the query * For better results, add all mapbox types to the query */ $wire->addHookAfter('FieldtypeGeocoder::filterQuery', function(HookEvent $event) { /** @var GeocodeQuery|ReverseQuery $query */ $query = $event->argumentsByName('query'); $query = $query->withData('location_type', Mapbox::TYPES); $event->return = $query; });
待办事项
- 如果使用输入字段的自定义功能或移动标记,请更新provider-string。
- 重构输入字段的javascript,以便用于其他地图或地图样式。
- 如果找不到供应商包,请添加警告!
反馈
如果您有任何反馈,请通过code@neuerituale.com与我们联系,或在github项目中创建一个问题。