/AddressFinder-Helper---Plugin-for-cakephp-2

A widget to enhance a cakephp form with address autocomplete, geocoding, and marker's drag'n'drop on the map with reverse geocoding. In human terms, an easier way for the user to find valid GoogleMaps addresses :)

Primary LanguagePHP

# AddressFinderHelper #

This is a CakePHP helper to add a widget their forms to locate addresses (and geolocate) on a google map;
 
javascript copied from.. well.. "heavily inspired" by:

http://tech.cibul.net/geocode-with-google-maps-api-v3/
This helper encampsulates the funtionalities of that script 
(address autocomplete, geococding, drag'n'drop reverse geocoding)
in an easy to use widget that can be seamlessy integrated in your forms

version 0.1.0

requirements: PHP5 / CakePhp 2.0
(should be easy to convert it to cakephp 1.3)

http://stefanomanfredini.info/2012/04/cakephp-2-0-address-finder-helper/

MIT License (http://www.opensource.org/licenses/mit-license.php)


## Features ##
The widget is an easy way to find and geocode addresses and places on the map.
Enhanching a form is easy and fast; it's just an helper, because everything is client-side. 
-the address field becomes a (jQueryUI) autocomplete, looking for addresses on google maps. 
-the selected address is geocoded, and the latitude and longitude fields are updated with the coordinates. (tehse fields  are optionally hidden o readonly)
-the marker on the map can be dragged, triggering a reverse geocoding that updates the address field too
-form submission can be optionally prevented if lat/lon are empty. 



## Usage: ##
1] (as a plugin in your app/Plugin directory or in CAKEROOT/plugins) load the plugin:
    in app/config/bootsrap.php add
    CakePlugin::load('AddressFinder');
    (or CakePlugin::loadAll();)

    include the helper in your controller (or in AppController if needed everywhere in your app) as usual;
    public $helpers = array([your helpers ...],'AddressFinder.AddressFinder');

    or simply copy the helper file in your app/View/Helper folder amd include it
    public $helpers = array([your helpers ...],'AddressFinder');



2] Optionally set your default options in bootstrap.php:

   $config['AddressFinder'] = array(
        //map settings
        'height' => '300px',
        'width' => '450px',
        'default' => array('lat' => '44.8378942', 'lon' => '11.6204396'),
        //form fields settings
        'modelName' => 'Place',
        'fields' => array('lat' => 'lat', 'lon' => 'lon', 'address' => 'address'), //match your db table fields when used for insert/update
        'latlonFieldsVisibility' => 'readonly', //'normal', 'readonly' or 'hidden' <- if hidden check your Security settings
        //rendering behaviour setting                
        'includeGoogleMapsScript' => true,
        'includeJQuery' => false,  //usually already included
        'renderFields' => false,  // render only the map and script, 
                                  // the form already has the required fields. If true, render the fields too
        'preventSubmit' => true    // no submit on empty lat/lon;
    );
  
   Configure::write('AddressFinder',$config['AddressFinder']);

3] in your view / element, call render() method (or directly renderMap) with custom options as needed
$options = array(
     'modelName' => 'MyModel',
     'fields' => array('lat' => 'latitude', 'lon' => 'longitude', 'address' => 'gmaps_address'),
     'renderFields' => true 
);
echo $this->AddressFinder->render($options);