toin0u / geocoder-laravel
Laravel 的地理编码服务提供商
Requires
- php: ^8.0
- geocoder-php/chain-provider: ^4.0
- geocoder-php/geo-plugin-provider: ^4.0
- geocoder-php/google-maps-provider: ^4.0
- guzzlehttp/psr7: *
- http-interop/http-factory-guzzle: ^1.0
- illuminate/cache: ^5.0|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
- illuminate/support: ^5.0|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
- php-http/curl-client: *
- willdurand/geocoder: ^4.0
Requires (Dev)
- doctrine/dbal: *
- geocoder-php/bing-maps-provider: ^4.0
- geocoder-php/geoip2-provider: ^4.0
- geocoder-php/maxmind-binary-provider: ^4.0
- laravel/legacy-factories: ^1.0
- orchestra/testbench: ^7.0|^8.0|^9.0
- orchestra/testbench-browser-kit: ^7.0|^8.5
- orchestra/testbench-dusk: ^7.0|^8.22
- php-coveralls/php-coveralls: *
- phpunit/phpunit: 8.5|^9.0|^10.5
- sebastian/phpcpd: ^5.0|^6.0
- dev-master
- 4.7.0
- 4.6.0
- 4.5.1
- 4.5.0
- 4.4.6
- 4.4.5
- 4.4.4
- 4.4.2
- 4.4.1
- 4.4.0
- 4.3.4
- 4.3.3
- 4.3.2
- 4.3.1
- 4.3.0
- 4.2.4
- 4.2.3
- 4.2.2
- 4.2.1
- 4.2.0
- 4.1.2
- 4.1.1
- 4.1.0
- 4.0.21
- 4.0.20
- 4.0.10
- 4.0.9
- 4.0.8
- 4.0.7
- 4.0.6
- 4.0.5
- 4.0.4
- 4.0.3
- 4.0.2
- 4.0.1
- 4.0.0
- 2.0.0-RC1
- 1.x-dev
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- 1.0.0-RC1
- 0.6.0
- 0.5.0
- 0.4.x-dev
- 0.4.1
- 0.4.0
- 0.3.0
- 0.2.0
- 0.1.0
- dev-develop
- dev-laravel-5.6
- dev-laravel-5.3
- dev-laravel-5.5
This package is auto-updated.
Last update: 2024-09-15 00:13:21 UTC
README
Laravel 地理编码器
如果你仍然在使用 Laravel 4,请查看 这里的 0.4.x 分支。
版本 4.0.0 是一个向后不兼容的更新。请在安装之前仔细阅读此文档,特别是 使用 部分。
此包允许你在 Laravel 5 中使用 Geocoder。
要求
- PHP >= 7.1.3
- Laravel >= 5.0
安装
- 通过 composer 安装此包
composer require toin0u/geocoder-laravel
- 如果你正在运行 Laravel 5.5(包将自动发现),请跳过此步骤。 在
config/app.php
中找到providers
数组键,并注册 Geocoder 服务提供商
// 'providers' => [ Geocoder\Laravel\Providers\GeocoderService::class, // ];
- 可选 我建议将以下行添加到你的
composer.json
文件中,以防止升级或更新包时在生产和开发环境中出现过时的缓存
"post-update-cmd": [ "@php artisan cache:clear", ], "post-install-cmd": [ "@php artisan cache:clear", ]
配置
如果你使用语言和区域值,请特别注意它们。例如,GoogleMaps 提供商使用 TLD 作为区域值,以下为语言值:https://developers.google.com/maps/faq#languagesupport。
此外,关于 GoogleMaps 提供商的特别说明:如果你使用 API 密钥,你也必须将 HTTPS 设置为 true。(最好是始终设置为 true,除非有特殊要求不这样做。)
有关可用适配器和提供者的列表,请参阅 Geocoder 文档。
推荐 专用缓存存储
要实现专用缓存存储,请在 config/database.php
中添加另一个 redis 存储条目,如下所示
"redis" => [ // ... "geocode-cache" => [ // choose an appropriate name 'host' => env('REDIS_HOST', '192.168.10.10'), 'password' => env('REDIS_PASSWORD', null), 'port' => env('REDIS_PORT', 6379), 'database' => 1, // be sure this number differs from your other redis databases ], ]
你还需要在 config/cache.php
中添加一个条目,将其指向此 redis 数据库
"stores" => [ // ... "geocode" => [ 'driver' => 'redis', 'connection' => 'geocode-cache', ], ],
最后,配置 Geocoder for Laravel 以使用此存储。编辑 config/geocoder.php
"cache" => [ "store" => "geocode", // ... ],
基于查询禁用缓存
你可以根据需要基于查询禁用缓存,如下所示
$results = app("geocoder") ->doNotCache() ->geocode('Los Angeles, CA') ->get();
提供者
如果你正在升级并且之前已发布地理编码配置文件,你需要添加 cache-duration
变量,否则缓存将禁用(默认为 0
缓存持续时间)。配置文件提供的默认缓存持续时间是 999999999
秒,基本上是永远。
默认情况下,配置指定了一个链式提供者,其中包含用于地址以及经纬度反向查找的 GoogleMaps 提供商,以及用于 IP 地址的 GeoIP 提供商。第一个返回结果将返回,后续提供者将不会执行。默认配置文件保持简洁,只包含这两个提供者。
但是,你可以根据需要添加或删除提供者,无论是在链式提供者内部还是在它旁边。以下是由包提供的默认配置
use Geocoder\Provider\Chain\Chain; use Geocoder\Provider\GeoPlugin\GeoPlugin; use Geocoder\Provider\GoogleMaps\GoogleMaps; use Http\Client\Curl\Client; return [ /* |-------------------------------------------------------------------------- | Cache Duration |-------------------------------------------------------------------------- | | Specify the cache duration in seconds. The default approximates a forever | cache, but there are certain issues with Laravel's forever caching | methods that prevent us from using them in this project. | | Default: 9999999 (integer) | */ 'cache-duration' => 9999999, /* |-------------------------------------------------------------------------- | Providers |-------------------------------------------------------------------------- | | Here you may specify any number of providers that should be used to | perform geocaching operations. The `chain` provider is special, | in that it can contain multiple providers that will be run in | the sequence listed, should the previous provider fail. By | default the first provider listed will be used, but you | can explicitly call subsequently listed providers by | alias: `app('geocoder')->using('google_maps')`. | | Please consult the official Geocoder documentation for more info. | https://github.com/geocoder-php/Geocoder#providers | */ 'providers' => [ Chain::class => [ GoogleMaps::class => [ env('GOOGLE_MAPS_LOCALE', 'us'), env('GOOGLE_MAPS_API_KEY'), ], GeoPlugin::class => [], ], ], /* |-------------------------------------------------------------------------- | Adapter |-------------------------------------------------------------------------- | | You can specify which PSR-7-compliant HTTP adapter you would like to use. | There are multiple options at your disposal: CURL, Guzzle, and others. | | Please consult the official Geocoder documentation for more info. | https://github.com/geocoder-php/Geocoder#usage | | Default: Client::class (FQCN for CURL adapter) | */ 'adapter' => Client::class, /* |-------------------------------------------------------------------------- | Reader |-------------------------------------------------------------------------- | | You can specify a reader for specific providers, like GeoIp2, which | connect to a local file-database. The reader should be set to an | instance of the required reader class or an array containing the reader | class and arguments. | | Please consult the official Geocoder documentation for more info. | https://github.com/geocoder-php/geoip2-provider | | Default: null | */ 'reader' => null, ];
适配器
默认情况下,我们提供了一个 CURL 适配器,以便你能够立即运行。但是,如果你已经安装了 Guzzle 或其他 PSR-7 兼容的 HTTP 适配器,我们鼓励你用它们替换 CURL 适配器。请参阅 Geocoder 文档 以获取具体实现细节。
自定义
如果您想更改默认配置,请发布并编辑配置文件
php artisan vendor:publish --provider="Geocoder\Laravel\Providers\GeocoderService" --tag="config"
用法
服务提供商初始化了 geocoder
服务,可通过外观 Geocoder::...
或应用程序助手 app('geocoder')->...
访问。
地址地理编码
获取地址集合
app('geocoder')->geocode('Los Angeles, CA')->get();
获取IP地址信息
app('geocoder')->geocode('8.8.8.8')->get();
反向地理编码
app('geocoder')->reverse(43.882587,-103.454067)->get();
导出结果
app('geocoder')->geocode('Los Angeles, CA')->dump('kml');
依赖注入
use Geocoder\Laravel\ProviderAndDumperAggregator as Geocoder; class GeocoderController extends Controller { public function getGeocode(Geocoder $geocoder) { $geocoder->geocode('Los Angeles, CA')->get() } }
升级
每次升级此包时,请记住清除您的缓存,以防止在引入破坏性更改时出现不兼容的缓存响应(这应该仅在主要版本中是必要的)
php artisan cache:clear
1.x 到 4.x
更新您的 composer.json 文件
"toin0u/geocoder-laravel": "^4.0",
请注意这里的一个变化是,从 Geocoder for Laravel
返回的结果现在使用 Laravel 原生的 Collections 类,而不是返回 AddressCollection
实例。这应该提供更大的灵活性来操作结果,并与使用 Laravel 的预期保持一致。现有的 AddressCollection
方法应直接映射到 Laravel 的 Collection
方法。但请确保仔细检查您的结果,如果您曾在结果上使用过 count()
、first()
、isEmpty()
、slice()
、has()
、get()
或 all()
。
此外,getProviders()
现在返回 Laravel Collection 而不是数组。
警告:如果您曾经使用过 getIterator()
方法,现在不再需要。只需像遍历任何其他 Laravel 集合一样遍历您的结果即可。
已弃用
- geocoder 上的
all()
方法正在被弃用,建议使用get()
,它将返回一个 Laravel Collection。然后您可以在该 Collection 上运行all()
。该方法将在 5.0.0 版本中删除。 - geocoder 上的
getProvider()
方法正在被弃用,建议使用getProviders()
,它将返回一个 Laravel Collection。然后您可以使用first()
在该 Collection 上获得相同的结果。该方法将在 5.0.0 版本中删除。
新增:此版本引入了创建更复杂查询的新方法
- geocodeQuery()
- reverseQuery()
有关详细信息,请参阅 Geocoder 文档。
0.x 到 1.x
如果您是从此包的 1.x 版本升级的,请记住以下事项
-
按照以下方式更新您的 composer.json 文件
"toin0u/geocoder-laravel": "^1.0",
-
删除您的
config/geocoder.php
配置文件。(如果您需要自定义它,请按照以下配置说明操作。) -
从您的
config/app.php
的别名部分删除任何 Geocoder 别名。(此包会自动注册别名。) -
将您的
config/app.php
中的服务提供商条目更新为Geocoder\Laravel\Providers\GeocoderService::class,
-
如果您在代码中使用外观,您有两个选择
-
将外观
Geocoder::
(并删除相应的use
语句)替换为app('geocoder')->
。 -
更新
use
语句为以下内容use Geocoder\Laravel\Facades\Geocoder;
-
-
更新您的查询语句以使用
->get()
(以检索 GeoCoder 对象的集合)或->all()
(以检索数组数组的数组),然后迭代以处理每个结果。
故障排除
- 清除缓存:
php artisan cache:clear
。 - 如果您仍然遇到困难,请请在 GitHub 上提交问题: https://github.com/geocoder-php/GeocoderLaravel/issues。
变更日志
https://github.com/geocoder-php/GeocoderLaravel/blob/master/CHANGELOG.md
贡献者行为准则
请注意,该项目是在 贡献者行为准则 下发布的。通过参与此项目,您同意遵守其条款。
许可证
GeocoderLaravel 在 MIT 许可证下发布。有关详细信息,请参阅捆绑的 LICENSE 文件。