Source for file google_geocoder.class.php

  1. <?php
  2.     /**
  3.     * This class accepts a street address and attempts to return lat/long coordinates using the Google Maps API.
  4.     * @author Richard Sharp
  5.     * @copyright 2007
  6.     * @version 1.0
  7.     * @package Location
  8.     */
  9.     
  10.     class GoogleGeocoder
  11.     {
  12.         /**
  13.         * Sets the API Key issued by Google.
  14.         *    @var string 
  15.         */
  16.         var $Key="";
  17.         
  18.         /**
  19.         * Sets the street address to geocode. If the address is not broken down into street, city, state, etc, put the entire address in this field.
  20.         *    @var string 
  21.         */
  22.         var $Address="";
  23.         
  24.         /**
  25.         * Sets the city to geocode.
  26.         *    @var string 
  27.         */
  28.         var $City="";
  29.         
  30.         /**
  31.         * Sets the state to geocode.
  32.         *    @var string 
  33.         */
  34.         var $State="";
  35.         
  36.         /**
  37.         * Sets the country to geocode.
  38.         *    @var string 
  39.         */
  40.         var $Country="US";
  41.         
  42.         /**
  43.         * Sets the zip code to geocode.
  44.         *    @var string 
  45.         */
  46.         var $Zip="";
  47.         
  48.         /**
  49.         * Gets the raw request sent to Google.
  50.         *    @var string 
  51.         */
  52.         var $LastRequest="";
  53.         
  54.         /**
  55.         * Gets the raw response received from Google.
  56.         *    @var string 
  57.         */
  58.         var $LastResponse;
  59.         
  60.          /**
  61.          * Constructor for the class.
  62.          * @param string The Google API key.
  63.          * @return void 
  64.          */ 
  65.         // class constructor
  66.         // $Key - the Google API key for this site
  67.         function GoogleGeocoder($Key)
  68.         {
  69.             $this->Key=$Key;
  70.         }
  71.         
  72.          /**
  73.          * Submits the address to Google and processes the response.
  74.          * @return mixed On success, returns an array containing two elements: Latitude and Longitude. Returns false on failure.
  75.          */ 
  76.         function GeoCode()
  77.         {
  78.             $objCurl curl_init()
  79.             
  80.             //build the parameters into the URL
  81.             $url="http://maps.google.com/maps/geo?q=".urlencode($this->Address).",+".urlencode($this->City).",+".urlencode($this->State).",+".$this->Country."&output=csv&key=".$this->Key;
  82.             
  83.             //Submit the request to Google
  84.             $this->LastRequest=$url;
  85.             curl_setopt($objCurlCURLOPT_POST,1)
  86.             curl_setopt($objCurlCURLOPT_URL,$url)
  87.             curl_setopt($objCurlCURLOPT_RETURNTRANSFER,1)
  88.             curl_setopt($objCurlCURLOPT_SSL_VERIFYPEERFALSE)
  89.             $LastResponse curl_exec ($objCurl);
  90.             $this->LastResponse=$LastResponse;
  91.             curl_close ($objCurl)
  92.             
  93.             //if successful, the response will be in this format: Specificity, Location, Latitude, Longitude
  94.             if(strpos($LastResponse,",")>0)
  95.             {
  96.                 $dLatitude=0;
  97.                 $dLongitude=0;
  98.                 $return_parts=explode("\n",$LastResponse);
  99.                 //there may be multiple rows returned. the last is the most accurate. loop through backwards until we find a positive response (200)
  100.                 while(($last_row=array_pop($return_parts)) && !$dLatitude)
  101.                 {
  102.                     $row_parts=explode(",",$last_row);
  103.                     if($row_parts[0]==200//200 is the specificity we want
  104.                     {
  105.                         $dLatitude=$row_parts[2];
  106.                         $dLongitude=$row_parts[3];
  107.                     }
  108.                 }
  109.             }
  110.             return(array("Latitude"=>$dLatitude,"Longitude"=>$dLongitude));
  111.         }
  112.     }
  113. ?>