From c7d12ac2905ff929b4512134e924ab2efdcb7828 Mon Sep 17 00:00:00 2001 From: "Michael J. Rubinsky" Date: Sun, 20 Dec 2009 16:01:19 -0500 Subject: [PATCH] Add initial, basic implementation for a Geonames.org geocoding client. --- .../lib/Horde/Ajax/Imple/Geocoder/Geonames.php | 68 ++++++++++++++++++++++ framework/Ajax/package.xml | 4 ++ 2 files changed, 72 insertions(+) create mode 100644 framework/Ajax/lib/Horde/Ajax/Imple/Geocoder/Geonames.php diff --git a/framework/Ajax/lib/Horde/Ajax/Imple/Geocoder/Geonames.php b/framework/Ajax/lib/Horde/Ajax/Imple/Geocoder/Geonames.php new file mode 100644 index 000000000..bdc3864e5 --- /dev/null +++ b/framework/Ajax/lib/Horde/Ajax/Imple/Geocoder/Geonames.php @@ -0,0 +1,68 @@ + + * @package Horde_Ajax + */ +class Horde_Ajax_Imple_Geocoder_Geonames extends Horde_Ajax_Imple_Base +{ + /** + * Constructor. + * + * @param array $params Configuration parameters. + *
+     * 'triggerId' => (string) [optional] TODO
+     * 
+ */ + public function __construct($params) + { + parent::__construct($params); + } + + /** + * Attach the object to a javascript event. + */ + public function attach() + { + } + + + /** + * Handle the geocoding request. + * + * @TODO: For reverse requests come up with a reasonable algorithm for + * checking if we have a lat/lng in the US since the + * findNearestAddress method is US only. If non-us, fallback to a + * findNearest or findPostalcode or similar request. Also will need + * to normalize the various response structures. + * + * $args['locations'] will trigger a forward geocoding request. + * $args['lat'] and $args['lon'] will trigger a reverse geocoding request. + * + * @see framework/Ajax/lib/Horde/Ajax/Imple/Horde_Ajax_Imple_Base#handle($args, $post) + * @throws Horde_Exception + */ + public function handle($args, $post) + { + if ($args['location']) { + $url = Horde_Util::addParameter('http://ws.geonames.org/searchJSON', 'q', $args['location']); + } elseif (!empty($args['lat']) && !empty($args['lon'])) { + $url = Horde_Util::addParameter('http:/ws.geonames.org/findNearestJSON', array('lat' => $args['lat'], 'lng' => $args['lon'])); + } + $client = new Horde_Http_Client(); + try { + $response = $client->get($url); + } catch (Horde_Http_Exception $e) { + throw new Horde_Exception($e); + } + return array('status' => 200, + 'results' => $response->getBody()); + } + +} diff --git a/framework/Ajax/package.xml b/framework/Ajax/package.xml index c70922004..39e2dfac2 100644 --- a/framework/Ajax/package.xml +++ b/framework/Ajax/package.xml @@ -36,6 +36,9 @@ http://pear.php.net/dtd/package-2.0.xsd"> + + + @@ -73,6 +76,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> + -- 2.11.0