Skip to content

Commit

Permalink
Revert "Abstract Geolocation (and use the Google Geocoding API instea…
Browse files Browse the repository at this point in the history
…d of the depreciated Google Maps API v2)"

This reverts commit 873d159.
  • Loading branch information
bshaffer committed Jun 15, 2011
1 parent cd39b75 commit e60f6d6
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 167 deletions.
127 changes: 0 additions & 127 deletions lib/GoogleGeoCoder.class.php

This file was deleted.

86 changes: 56 additions & 30 deletions lib/template/Geolocatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,30 @@
* @package Locatable_Extension
* @subpackage template
* @author Brent Shaffer
* @author Matt Farmer <work@mattfarmer.net>
* @copyright Copyright (c) 2008 Centre{source}, Brent Shaffer 2008-12-22. All rights reserved.
*/
class Doctrine_Template_Geolocatable extends Doctrine_Template
{
/**
* Array of locatable options
*/
protected $_options = array(
'columns' => array(
'latitude' => array(
'name' => 'latitude',
'type' => 'double',
'length' => 16,
'alias' => null,
'options' => array('length' => 16, 'scale' => 10)),
'longitude' => array(
'name' => 'longitude',
'type' => 'double',
'length' => 16,
'alias' => null,
'options' => array('length' => 16, 'scale' => 10)),
),
'fields' => array(),
'distance_unit' => 'miles',
'geocoder_class' => 'GoogleGeoCoder',
);
protected $_url = 'http://maps.google.com/maps/geo',
$_options = array('columns' => array(
'latitude' => array(
'name' => 'latitude',
'type' => 'double',
'length' => 16,
'alias' => null,
'options' => array('length' => 16, 'scale' => 10)),
'longitude' => array(
'name' => 'longitude',
'type' => 'double',
'length' => 16,
'alias' => null,
'options' => array('length' => 16, 'scale' => 10)),
), 'fields' => array(),
'distance_unit' => 'miles',
);


/**
Expand Down Expand Up @@ -74,25 +71,54 @@ public function setTableDefinition()
$this->addListener(new Doctrine_Template_Listener_Geolocatable($this->_options));
}


public function refreshGeocodes()
// =======================
// = Geocoding Functions =
// =======================
public function buildGeoQuery()
{
$obj = $this->getInvoker();

$obj = $this->getInvoker();
$query = array();
foreach ($this->_options['fields'] as $field)
foreach ($this->_options['fields'] as $field)
{
$query[] = $obj->$field;
}

$geocoder_class = $this->_options['geocoder_class'];
$geocoder = new $geocoder_class(implode(', ', $query));
return implode(', ', array_filter($query));
}

public function buildUrlFromQuery($query)
{
return $this->_url
. '?'
. http_build_query(array('q' => $query, 'output' => 'csv'));
}

public function retrieveGeocodesFromUrl($url)
{
$codes = explode(',', file_get_contents($url));
$geocodes = array('latitude' => null, 'longitude' => null);

if (count($codes) >= 4)
{
$geocodes['latitude'] = $codes[2];
$geocodes['longitude'] = $codes[3];
}

return $geocodes;
}

public function refreshGeocodes($url = null)
{
$obj = $this->getInvoker();

foreach($this->_options['columns'] as $key => $options )
if (!$url)
{
$func = 'get'.sfInflector::camelize($options['name']);
$obj[$options['name']] = $geocoder->$func();
$url = $this->buildUrlFromQuery($this->buildGeoQuery());
}

$geocodes = $this->retrieveGeocodesFromUrl($url);
$obj[$this->_options['columns']['latitude']['name']] = $geocodes['latitude'];
$obj[$this->_options['columns']['longitude']['name']] = $geocodes['longitude'];
}

public function addDistanceQueryTableProxy($query, $latitude, $longitude, $distance = null)
Expand Down
15 changes: 5 additions & 10 deletions test/unit/GeolocatableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
$article->zip = 37211;
$article->save();

$t->info(" Verify at http://maps.google.com/maps?hl=en&q={$article->latitude},+{$article->longitude}");
$t->is($article->latitude, 36.0558177);
$t->is($article->latitude, 36.0775432);
$t->is($article->longitude, -86.7315785);

$t->info('Test Geolocatable Plugin locates with missing fields');
Expand All @@ -24,8 +23,7 @@
$article->zip = 37211;
$article->save();

$t->info(" Verify at http://maps.google.com/maps?hl=en&q={$article->latitude},+{$article->longitude}");
$t->is($article->latitude, 36.0558177);
$t->is($article->latitude, 36.0775432);
$t->is($article->longitude, -86.7315785);

$t->info('Test Geolocatable Plugin locates by address');
Expand All @@ -35,9 +33,8 @@
$article->address = 'Tour Eiffel Champ de Mars 75007 Paris, France';
$article->save();

$t->info(" Verify at http://maps.google.com/maps?hl=en&q={$article->latitude},+{$article->longitude}");
$t->is($article->latitude, 48.8582635);
$t->is($article->longitude, 2.2942543);
$t->is($article->latitude, 48.8582780);
$t->is($article->longitude, 2.2942540);

$t->info('Test Geolocatable Plugin updates latitude / longitude automatically when saved');

Expand All @@ -49,15 +46,13 @@
$article->save();


$t->info(" Verify at http://maps.google.com/maps?hl=en&q={$article->latitude},+{$article->longitude}");
$t->is($article->latitude, 36.0558177);
$t->is($article->latitude, 36.0775432);
$t->is($article->longitude, -86.7315785);

// Latitude / Longitude data changes with new zipcode
$article->zip = 37212;
$article->save();

$t->info(" Verify at http://maps.google.com/maps?hl=en&q={$article->latitude},+{$article->longitude}");
$t->is($article->latitude, 36.1281626);
$t->is($article->longitude, -86.7969244);

Expand Down

0 comments on commit e60f6d6

Please sign in to comment.