Location: PHPKode > scripts > Geocoder > geocoder/GoogleGeocoder.inc
<?php
/**
 * Google Geocoder for World Cities
 *
 * @author Jan Bruyndonckx
 * @copyright Copyright (c) 2008 Banzora   http://software.banzora.com
 * @package main
 * @subpackage lib
 *
*/
require_once "Geocoder.inc";
require_once "Countries.inc";

/**
	Class to geocode a Worldwide location.
	@see http://www.google.com/apis/maps/documentation
*/

class GoogleGeocoder extends Geocoder
{
    const LOCAL_KEY = 'ABQIAAAAQm3KbGwD2-pr_BlTIY7TxhT2yXp_ZAY8_ufC3CFXhHIE1NvwkxQJq13kuJoWspBBG7gEt8RJVl2OlQ' ;
    const SERVICE_URL = "http://maps.google.com/maps/geo?" ;

    protected function createRequest () {
        global $countries ;

        $url = GoogleGeocoder::SERVICE_URL ;

        $key = self::LOCAL_KEY ; // << replace by your key
        $output = 'csv' ;

        if ($this->country == 'US') {
            $q = $this->city ;
            if ($this->state)   $q .= ','.$this->state ;
            if ($this->zipcode) $q .= ','.$this->zipcode ;
            $q .= ',USA' ;
        } else {
            if (isset ($countries[$this->country])) {
                $this->country = urlencode($countries[$this->country]) ;
            }
            if ($this->zipcode) {
                $q = $this->city.','. $this->zipcode . ',' . $this->country ;
            } else {
                $q = $this->city.','.$this->country ;
            }
        }
        $url .= 'key='.$key.'&output='.$output.'&q='.$q ;

        //echo("url=$url<br>");
        return $url ;
    }

    /**--------------------------------------------------------------------
    Parses a result that looks like:
    200,6,42.730070,-73.690570  => error code, accuracy, latitude, longitude
    */
    public function parseResult ($result)
    {
        $result = explode (',', $result) ;
        if (count($result) != 4) {
            return $this->createResult (NULL, NULL, "no_geocode") ;
        }
        $error = trim ($result[0]) ;
        //$accuracy = trim($result[1]) ;
        $lat = trim ($result[2]) ;
        $lon = trim ($result[3]) ;

        if ($error != 200)
        {
            return $this->createResult (NULL, NULL, "no_geocode") ;
        }

        return $this->createResult ($lon, $lat) ;
    }
}
// end class GoogleGeocoder

?>
Return current item: Geocoder