承接 chris48s/cakephp-geocoder 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

chris48s/cakephp-geocoder

Composer 安装命令:

composer require chris48s/cakephp-geocoder

包简介

Geocoder controller component and model behavior

README 文档

README

A thin wrapper around the Google Maps Geocoding API.

This plugin adds a geocoder controller component and model behavior to your CakePHP 3 application. It is based on Martin Bean's CakePHP 2 Geocoder Plugin, updated for compatibility with CakePHP 3. Thanks to Martin for making his code available under a MIT licence.

Installation

Install from packagist using composer. Add the following to your composer.json:

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

and run composer install or composer update, as applicable.

Loading the plugin

Add the code Plugin::load('Chris48s/Geocoder'); to your bootstrap.php.

Using the Component

You can use the component to geocode addresses within your controllers. A good example is if you need to take a user-submitted value and convert it to a latitude/longitude pair to pass to a model to search it.

To geocode an address in your controllers, do something similar to the following:

<?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);
        }
    }
}

The component will return a response as a native PHP object from Google’s Geocoding API.

Using the Behavior

There is also a model behavior. This is useful if saving a record and you want to create a latitude/longitude pair from a field in your model’s data that represents the address, for example a store. Attach the behavior in your table class:

<?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');
    }
}

Configuration

By default, the behavior assumes you have two columns in your corresponding database table called latitude and longitude, and also a column called address. These can be changed though. Simply pass an array of options when attaching the behavior:

<?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'
        ]);
    }
}

The addressColumn key also accepts an array. If you pass an array for the value, then the behavior will iterate over the fields and assemble the address that way. So if you store addresses as their separate components then you could do the following:

<?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'
            ]
        ]);
    }
}

If addressColumn is not a string or an array, the Behavior will throw an exception of class GeocoderException.

By default, the behavior will not save the address if geocoding fails. This can also be configured using the requireSuccess key. Set this to false to save the address anyway even if geocoding fails:

<?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,
        ]);
    }
}

If using this setting, your latitudeColumn and longitudeColumn should be set to allow NULL.

API Keys

To use the plugin with a Google API key, obtain a key and pass it in when calling the component

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

or when configuring the behaviour

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

Error Handling

If the Google Maps Geocoding API returns a status code other than 200 OK, the Component will throw an exception of class GeocoderException. The Behavior will add a validation error to the target entity object which can be accessed using $entity->errors().

Reporting Issues

If you have any issues with this plugin then please feel free to create a new Issue on the GitHub repository. This plugin is licensed under the MIT License.

统计信息

  • 总下载量: 514
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 2
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-01-29

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固