diff options
author | Evan Prodromou <evan@status.net> | 2009-09-16 11:46:10 -0400 |
---|---|---|
committer | Evan Prodromou <evan@status.net> | 2009-10-21 22:47:19 -0400 |
commit | 7b227fd1c06ff6f3ba078b73348dd02419d44e38 (patch) | |
tree | 486b856b8a4a301eee63d08bd2542461b5f059d5 /lib/location.php | |
parent | b6b245dc0be75a3358fee560a40a54e280e4f2a7 (diff) |
start getting locations from remote services
Diffstat (limited to 'lib/location.php')
-rw-r--r-- | lib/location.php | 54 |
1 files changed, 53 insertions, 1 deletions
diff --git a/lib/location.php b/lib/location.php index cbd03f33a..f4ce7f67a 100644 --- a/lib/location.php +++ b/lib/location.php @@ -50,11 +50,63 @@ class Location var $names; - static function fromName($name, $language=null) + const geonames = 1; + const whereOnEarth = 2; + + static function fromName($name, $language=null, $location_ns=null) { if (is_null($language)) { $language = common_language(); } + if (is_null($location_ns)) { + $location_ns = common_config('location', 'namespace'); + } + + $location = null; + + if (Event::handle('LocationFromName', array($name, $language, $location_ns, &$location))) { + + switch ($location_ns) { + case Location::geonames: + return Location::fromGeonamesName($name, $language); + break; + case Location::whereOnEarth: + return Location::fromWhereOnEarthName($name, $language); + break; + } + } + + return $location; + } + + static function fromGeonamesName($name, $language) + { + $location = null; + $client = HTTPClient::start(); + + // XXX: break down a name by commas, narrow by each + + $str = http_build_query(array('maxRows' => 1, + 'q' => $name, + 'lang' => $language, + 'type' => 'json')); + + $result = $client->get('http://ws.geonames.org/search?'.$str); + + if ($result->code == "200") { + $rj = json_decode($result->body); + if (count($rj['geonames']) > 0) { + $n = $rj['geonames'][0]; + $location = new Location(); + $location->lat = $n->lat; + $location->lon = $n->lon; + $location->name = $n->name; + $location->location_id = $n->geonameId; + $location->location_ns = Location:geonames; + } + } + + return $location; } static function fromId($location_id, $location_ns = null) |