Permalink
Browse files

Upgraded Google geocoder to API v3

  • Loading branch information...
1 parent 99292f3 commit 8f75da5eec44a276f7a40da59f33d9555596e5b3 Rob Tuley committed Sep 13, 2011
Showing with 22 additions and 44 deletions.
  1. +14 −29 i18n/Google/GeoCode.php
  2. +8 −15 i18n/Test/Google/GeoCode.php
View
43 i18n/Google/GeoCode.php
@@ -1,14 +1,5 @@
<?php
/**
- * Defines the T_Google_Geocode class.
- *
- * @package geo
- * @author Rob Tuley
- * @version SVN: $Id$
- * @license http://knotwerk.com/licence MIT
- */
-
-/**
* Uses the Google Maps API to geocode a point.
*
* @package geo
@@ -18,13 +9,6 @@ class T_Google_GeoCode extends T_Filter_Skeleton implements T_Filter_GeoCode
{
/**
- * Your google maps key.
- *
- * @var string
- */
- protected $key;
-
- /**
* Whether you are using a GPS sensor.
*
* @var bool
@@ -45,10 +29,9 @@ class T_Google_GeoCode extends T_Filter_Skeleton implements T_Filter_GeoCode
* @param bool $sensor whether you are using a sensor (e.g. GPS) to fix user position
* @param T_Filter $prior prior filter
*/
- function __construct($key,$sensor=false,$prior=null)
+ function __construct($sensor=false,$prior=null)
{
parent::__construct($prior);
- $this->key = $key;
$this->sensor = (bool) $sensor;
}
@@ -78,12 +61,9 @@ function biasToCountry($code)
protected function doTransform($value)
{
// build url
- $url = 'http://maps.google.com/maps/geo?q='.urlencode($value).
- '&output=csv'.
- '&key='.urlencode($this->key).
- '&oe=utf8'.
+ $url = 'http://maps.googleapis.com/maps/api/geocode/json?address='.urlencode($value).
'&sensor='.($this->sensor ? 'true' : 'false');
- if ($this->cc_tld!==false) $url .= '&gl='.urlencode($this->cc_tld);
+ if ($this->cc_tld!==false) $url .= '&region='.urlencode($this->cc_tld);
// make request
// (set up a CURL request, telling it not to spit back headers)
@@ -93,16 +73,21 @@ protected function doTransform($value)
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$data = curl_exec($ch);
+ $code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
curl_close($ch);
// process data
- // (response_code,accuracy,longitude,latitude)
- if (substr($data,0,3)==='200') {
- $data = explode(',',$data);
- $point = new T_Geo_Point($data[3],$data[2]);
- } else {
- throw new T_Exception_Filter("Failed to geocode $value with response $data");
+ $point = false;
+ if ($code==200) {
+ $data = json_decode($data,true);
+ if ($data['status']=='OK') {
+ $geo = reset($data['results']);
+ $geo = $geo['geometry']['location'];
+ $point = new T_Geo_Point($geo['lng'],$geo['lat']);
+ }
}
+ if (!$point)
+ throw new T_Exception_Filter("Failed to geocode $value");
return $point;
}
View
23 i18n/Test/Google/GeoCode.php
@@ -1,28 +1,23 @@
<?php
class T_Test_Google_GeoCode extends T_Unit_Case
{
- protected $key;
- function setUpSuite()
+ function setUp()
{
- $this->key = $this->getFactory()->getGoogleMapKey();
- }
-
- function requiresKey()
- {
- if (!$this->key) $this->skip('Requires a Google Maps API key');
+ if (!$this->getFactory()->isNetwork()) {
+ $this->skip('Network must be enabled in config to test Google geocode');
+ }
}
function testBiasByCountryCodeHasAFluentInterface()
{
- $filter = new T_Google_GeoCode('key');
+ $filter = new T_Google_GeoCode;
$this->assertSame($filter,$filter->biasToCountry('GB'));
}
function testCanGeocodeATownWithoutCountryBiasAndNoSensor()
{
- $this->requiresKey();
- $filter = new T_Google_GeoCode($this->key);
+ $filter = new T_Google_GeoCode;
$point = $filter->transform('1600 amphitheatre mountain view ca');
$this->assertTrue($point instanceof T_Geo_Point);
$this->assertSimilarFloat(-122.0841430,$point->getLongitude(),0.5);
@@ -31,8 +26,7 @@ function testCanGeocodeATownWithoutCountryBiasAndNoSensor()
function testGeoCodingFailureResultsInException()
{
- $this->requiresKey();
- $filter = new T_Google_GeoCode($this->key);
+ $filter = new T_Google_GeoCode;
try {
$filter->transform(''); // expect 601 = G_GEO_MISSING_QUERY response
$this->fail();
@@ -43,8 +37,7 @@ function testGeoCodingFailureResultsInException()
function testCanBiasGeocodedResultsToACountry()
{
- $this->requiresKey();
- $filter = new T_Google_GeoCode($this->key,true);
+ $filter = new T_Google_GeoCode(true);
// defaults to Toledo, OH, USA
$point = $filter->transform('Toledo');

0 comments on commit 8f75da5

Please sign in to comment.