chris48s/cakephp-geocoder

地理编码控制器组件和模型行为

安装: 483

依赖项: 0

建议者: 0

安全: 0

星标: 1

关注者: 3

分支: 2

公开问题: 0

类型:cakephp-plugin

1.0.1 2017-12-06 17:45 UTC

This package is auto-updated.

Last update: 2024-09-24 13:17:54 UTC


README

Google Maps 地理编码API的轻量级包装器。

此插件为您的 CakePHP 3 应用程序添加了地理编码控制器组件和模型行为。它基于 Martin Bean 的 CakePHP 2 地理编码插件,并已更新以兼容 CakePHP 3。感谢 Martin 将其代码在 MIT 许可下提供。

安装

使用 composerpackagist 安装。将以下内容添加到您的 composer.json

"require": {
    "chris48s/cakephp-geocoder": "^1.0.0"
}

然后运行 composer installcomposer update,根据适用情况。

加载插件

Plugin::load('Chris48s/Geocoder'); 代码添加到您的 bootstrap.php 中。

使用组件

您可以使用此组件在控制器中对地址进行地理编码。一个很好的例子是如果您需要获取用户提交的值并将其转换为经纬度对,以便传递给模型进行搜索。

在控制器中地理编码地址,可以执行如下操作

<?php
namespace App\Controller;

use App\Controller\AppController;

class StoresController extends AppController
{

    public function initialize()
    {
        parent::initialize();
        $this->loadComponent('Chris48s/Geocoder.Geocoder');
    }

    public function search()
    {

        $location = $this->request->query['location'];

        $geocodeResult = $this->Geocoder->geocode($location);

        if (count($geocodeResult) > 0) {
            $latitude = floatval($geocodeResult[0]->geometry->location->lat);
            $longitude = floatval($geocodeResult[0]->geometry->location->lng);
        }
    }
}

组件将返回一个来自 Google 地理编码 API 的原生 PHP 对象作为响应。

使用行为

还有一个模型行为。如果您在保存记录时想从模型数据中的地址字段创建一个经纬度对,例如商店,则很有用。在您的表类中附加行为

<?php
namespace App\Model\Table;

use Cake\ORM\Table;

class Stores extends Table
{

    public function initialize(array $config)
    {
        parent::initialize($config);
        $this->addBehavior('Chris48s/Geocoder.Geocodable');
    }
}

配置

默认情况下,行为假定您的对应数据库表中有两个名为 latitudelongitude 的列,还有一个名为 address 的列。这些可以更改。只需在附加行为时传递一个选项数组。

<?php
namespace App\Model\Table;

use Cake\ORM\Table;

class Stores extends Table
{

    public function initialize(array $config)
    {
        parent::initialize($config);
        $this->addBehavior('Chris48s/Geocoder.Geocodable', [
            'addressColumn' => 'street_address',
            'latitudeColumn' => 'lat',
            'longitudeColumn' => 'lng'
        ]);
    }
}

addressColumn 键也接受一个数组。如果您传递一个数组作为值,那么行为将遍历字段并按这种方式组装地址。所以如果您按各自的组件存储地址,则可以执行以下操作

<?php
namespace App\Model\Table;

use Cake\ORM\Table;

class Stores extends Table
{

    public function initialize(array $config)
    {
        parent::initialize($config);
        $this->addBehavior('Chris48s/Geocoder.Geocodable', [
            'addressColumn' => [
                'street_address',
                'locality',
                'postal_code'
            ]
        ]);
    }
}

如果 addressColumn 不是一个字符串或数组,行为将抛出一个 GeocoderException 类的异常。

默认情况下,如果地理编码失败,行为将不会保存地址。这也可以使用 requireSuccess 键进行配置。将其设置为 false 以便即使地理编码失败也保存地址。

<?php
namespace App\Model\Table;

use Cake\ORM\Table;

class Stores extends Table
{

    public function initialize(array $config)
    {
        parent::initialize($config);
        $this->addBehavior('Chris48s/Geocoder.Geocodable', [
            'requireSuccess' => false,
        ]);
    }
}

如果使用此设置,您的 latitudeColumnlongitudeColumn 应设置允许 NULL。

API 密钥

要使用 Google API 密钥与插件一起使用,请获取密钥并在调用组件或配置行为时传递。

$this->loadComponent('Chris48s/Geocoder.Geocoder');
$geocodeResult = $this->Geocoder->geocode($location, ['key' => 'my-api-key']);

或当配置行为时

$this->addBehavior('Chris48s/Geocoder.Geocodable', [
    'parameters' => ['key' => 'my-api-key']
]);

错误处理

如果 Google Maps 地理编码 API 返回的状态代码不是 200 OK,组件将抛出一个 GeocoderException 类的异常。行为将向目标实体对象添加验证错误,可以使用 $entity->errors() 访问。

问题报告

如果您对此插件有任何问题,请随时在 GitHub 仓库 上创建一个新的 问题。此插件受 MIT 许可证的许可。