the most awesome guy ever.

The Blog of Darryl E. Clarke

  Random musings from a jaded coder who just needs a hug.

Geocodes Made Easy

Using my Google Maps API key I can geocode almost anything*.

And with Zend Framework, it’s f’n damn simple!

Zend_Loader::loadClass('Zend_Rest_Client');
$rest = new Zend_Rest_Client('http://maps.google.com/maps/geo');
//?q=1600+Amphitheatre+Parkway,+Mountain+View,+CA&output=json&oe=utf8&sensor=true_or_false&key=your_api_key

$rest->key('YOUR_API_KEY');
$rest->q('1600 Amphitheatre Parkway, Mountain View, CA');
$rest->output('xml');
$rest->oe('utf8');

$result =  $rest->get();
// the whole $result is a SimpleXML object
print_r((string)$result->Response->Placemark->Point->coordinates);

And that’s it.  Save the results someplace useful so you don’t have to hammer the shizzle out of Google (there are query limitations per key).

* I mean almost anything, because you cannot geocode a UK Postal Code, because the Queen and her Royal Mail owns the copyrights on it. Silly, I know.

Tags: , , ,