falco442 / gps
Gps plugin for CakePHP
Installs: 6
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Type:cakephp-plugin
Requires
- php: >=5.4.16
- cakephp/cakephp: ~3.0
Requires (Dev)
This package is not auto-updated.
Last update: 2024-11-23 19:48:37 UTC
README
This plugin enable the search for some entity, given its coordinates.
Requirements:
- CakePHP 3.x
Installation
Table entry
Add the fields (choose your names, or use the default 'latitude' and 'longitude'). I suggest to use the FLOAT(10,6)
. Note that, if you change the field names, you must provide them in the plugin configuration.
Setting up the code
Add in config/bootstrap.php
the line
Plugin::load('Gps');
and in src/Model/Table/YourModelTable.php
the lines
class ArticlesTable extends Table { public function initialize(array $config) { ... $this->addBehavior('Gps.Gps'); ... } }
You can also provide a configuration with the plugin, extending this lines:
class ArticlesTable extends Table { public function initialize(array $config) { ... $this->addBehavior('Gps.Gps',[ 'radius'=>6896000000.231, 'fields'=>[ 'latitude'=>'lat', 'longitude'=>'lon' ] ]); ... } }
For example, in this configuration, we're informing the plugin that the fields in the table will be of names 'lat' for latitude, and 'lon' for longitude. The Earth radius will be of 6896000000.231
km.
Usage
in src/Controller/YourModelController
, for example, in a nearBy
action, you could have
public function nearBy(){ $yourmodel = $this->YourModel->find('near',['gps'=>$this->request->query]); $this->set(compact('yourmodel')); $this->set('_serialize',['yourmodel']); }
and so the conditions will be the ones urlencoded:
http://yourdomain.com/youmodel/near-by.json?latitude=43.2&longitude=24.356&radius=20000
With this url we're telling ours API software to take the objects in a square with center in 43.2
,24.35
and side of 20000
meters