toin0u/geocoder-laravel

Laravel 的地理编码服务提供商


README

Travis Scrutinizer Coveralls GitHub release Packagist

Laravel 地理编码器

如果你仍然在使用 Laravel 4,请查看 这里的 0.4.x 分支

版本 4.0.0 是一个向后不兼容的更新。请在安装之前仔细阅读此文档,特别是 使用 部分。

此包允许你在 Laravel 5 中使用 Geocoder

要求

  • PHP >= 7.1.3
  • Laravel >= 5.0

安装

  1. 通过 composer 安装此包
composer require toin0u/geocoder-laravel
  1. 如果你正在运行 Laravel 5.5(包将自动发现),请跳过此步骤。config/app.php 中找到 providers 数组键,并注册 Geocoder 服务提供商
// 'providers' => [
    Geocoder\Laravel\Providers\GeocoderService::class,
// ];
  1. 可选 我建议将以下行添加到你的 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 版本升级的,请记住以下事项

  1. 按照以下方式更新您的 composer.json 文件

    "toin0u/geocoder-laravel": "^1.0",
  2. 删除您的 config/geocoder.php 配置文件。(如果您需要自定义它,请按照以下配置说明操作。)

  3. 从您的 config/app.php 的别名部分删除任何 Geocoder 别名。(此包会自动注册别名。)

  4. 将您的 config/app.php 中的服务提供商条目更新为

    Geocoder\Laravel\Providers\GeocoderService::class,
  5. 如果您在代码中使用外观,您有两个选择

    1. 将外观 Geocoder::(并删除相应的 use 语句)替换为 app('geocoder')->

    2. 更新 use 语句为以下内容

      use Geocoder\Laravel\Facades\Geocoder;
  6. 更新您的查询语句以使用 ->get()(以检索 GeoCoder 对象的集合)或 ->all()(以检索数组数组的数组),然后迭代以处理每个结果。

故障排除

变更日志

https://github.com/geocoder-php/GeocoderLaravel/blob/master/CHANGELOG.md

贡献者行为准则

请注意,该项目是在 贡献者行为准则 下发布的。通过参与此项目,您同意遵守其条款。

许可证

GeocoderLaravel 在 MIT 许可证下发布。有关详细信息,请参阅捆绑的 LICENSE 文件。