CakePHP Geocoding behavior is a CakePHP behavior which helps finding lat and lng in Google Map from a given zipcode or address. Before you start reading, make sure you have good understanding of CakePHP behavior, and at least have experience using CakePHP behavior. If not, we recommend you to read Cook Book behavior first. In this tutorial we will introduce you Geocoding behavior as well as demonstrate a simple example.
CREATE TABLE `places` (
`id` int(11) NOT NULL auto_increment,
`lat` float(10,6) NOT NULL,
`lng` float(10,6) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
COLLATE=latin1_general_ci AUTO_INCREMENT=1 ;
Note: We have defined float(10,6) to store both lat and lng.
Place your downloaded geocoding.php into "app/models/behaviors/geocoding.php".
Create your place model "app/models/place.php" and include Geocoding behavior:
<?php
class Place extends AppModel {
public $actsAs = array('Geocoding');
}
?>
If you don't supply any parameters to this behavior, it will try to geocode lat and lng from $this->data['Place']['address'].
public $actsAs =
array('Geocoding'=>
array(
'address'=>'',
'lat_field'=>'',
'lng_field'=>''
)
);
If you like our tutorial, please follow us on Twitter and help spread the word. We need your support to continue.