Search code examples
geocodingbehaviorcakephp-1.2

Cakephp Geocoding Behaviour


I have been following this tutorial. Basically I want to take an address from a table I have and place that address on a google map. The tutorial seems pretty straight forward, create the table places, put the behavior in and model and what not.

Just kind of confused how I'd actually use that with my existing tables. I've read through the section in the cookbook about behaviors but am still a little confused about them. Mostly about how I'd integrate the tutorial into other views and controllers that aren't within place model?


Solution

  • first you need to add latitude / longitude coordinates to your address table...

    you can do that with a writing parser around this google maps api call:

    http://maps.google.com/maps/api/geocode/xml?address=miami&sensor=false

    once your addresses contain the coordinates, all you need is to pass the coordinates to a javascript google maps in your view, just copy the source :

    http://code.google.com/apis/maps/documentation/javascript/examples/map-simple.html

    here is a cakephp 2.0 shell to create latitude / longitude data

    to run: "cake geo"

    GeoShell.php :

    <?php
    class GeoShell extends Shell {
    
    public $uses = array('Business');
    
    public function main() {
    
    
    
        $counter = 0;
        $unique = 0;
    
    
        $temps = $this->Business->find('all', array(
            'limit' => 100
        ));
    
        foreach($temps as $t) {
    
                $address = $this->geocodeAddress($t['Business']['address']);
    
                print_r($address);
    
                if(!empty($address)) {
                    $data['Business']['id'] = $t['Business']['id'];
                    $data['Business']['lat'] = $address['latitude'];
                    $data['Business']['lng'] = $address['longitude'];
    
                    $this->Business->save($data, false);
                    $unique++;
                }
    
        }
    
        $counter++;
    
    
        $this->out(' - Processed Records: ' . $counter . ' - ');
        $this->out(' - Inserted Records: ' . $unique . ' - ');
    
    }
    
    
    public function geocodeAddress($address) {
    
        $url = 'http://maps.google.com/maps/api/geocode/json?address=' . urlencode($address) . '&sensor=false';
    
        $response = @file_get_contents($url);
    
        if($response === false) {
            return false;
        }
    
        $response = json_decode($response);
    
        if($response->status != 'OK') {
            return false;
        }
    
        foreach ($response->results['0']->address_components as $data) {
            if($data->types['0'] == 'country') {
                $country = $data->long_name;
                $country_code = $data->short_name;
            }
        }
    
        $result = array(
            'latitude'  => $response->results['0']->geometry->location->lat,
            'longitude' => $response->results['0']->geometry->location->lng,
            'country' => $country,
            'country_code' => $country_code,
            'googleaddress' => $response->results['0']->formatted_address,
        );
        return $result;
    }
    

    }