Google Geocoding with CakePHP
class FakeController extends AppController { var $components = array("Googlegeocode"); /* functions and whatever other code ... */ function getGeoData() { $address = $this->data["ModelName"]["address"]; $coords = NULL; if($address) { $coords = $this->Googlegeocode->getCoords($address); } $this->set("coords", $coords); } // End of function } // End of classThis will return an array built directly from the XML returned by Google. From this you can extract all of the information they typically return, including status, address information and geodata as well as several other odds and ends. There is actually quite a bit of data returned for each address. Two other useful functions are the lastCoords() and lastGeodataRecord() functions. They are called as follows:
1
2
3
4
5
6
7
8
9
10
There is more code there in general class setup and comments than there is in actually making the coordinate request. Note, do not URL encode your address before passing it into the function. This can have unexpected results as the geocoding component will properly encode the address for you.
There are a couple of other functions in case you need them. First is a call to retrieve the data set which is returned from Google.
<pre class="brush:php">
// ... code ...
$geodataRecord =
$this->Googlegeocode->getGeodataRecord($address);
// ... code ...// ... code ... $coords = $this->Googlegeodata->lastCoords(); $geodataRecord = $this->Googlegeodata->lastGeodataRecord(); // ... code ... ``` Once a record is retrieved, it is stored in memory until a new record is requested. You can refer to these as needed to recall the latest records retrieved from Google until the script finishes executing. Though this isn't the typical user experience related post, hopefully this will help you get moving more quickly on your project involving geocoding addresses for use with the Google Maps UI API. I hope you find my component useful and you use it to make the web a better place.