codificar/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 提供者使用 TLDs 作为区域值,以下为语言值: 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 以使用此存储。编辑 config/geocoder.php

    "cache" => [
        "store" => "geocode",

        // ...
    ],

提供者

如果你正在升级并且之前已发布地理编码配置文件,你需要添加 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', 'en-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。然后您可以在该集合上运行 all()。此方法将在版本 5.0.0 中删除。
  • geocoder 上的 getProvider() 方法正在弃用,建议使用 getProviders(),它将返回 Laravel Collection。然后您可以使用 first() 在该集合上获取相同的结果。此方法将在版本 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