Permalink
Browse files

initial commit

  • Loading branch information...
1 parent a1915b9 commit e10c3fe2cf28f7940065b8e6e3395e87a2246665 straup committed with Oct 25, 2010
Showing with 89 additions and 0 deletions.
  1. +89 −0 include/lib_geo_geocode.php
@@ -0,0 +1,89 @@
+<?php
+
+ #
+ # $Id$
+ #
+
+ #################################################################
+
+ loadlib("http");
+
+ #################################################################
+
+ function geo_geocode_service_map($string_keys=0){
+
+ # 0 means 'not geocoded'
+
+ $map = array(
+ 1 => 'yahoo',
+ );
+
+ if ($string_keys){
+ $map = array_flip($map);
+ }
+
+ return $map;
+ }
+
+ #################################################################
+
+ function geo_geocode_string($string){
+
+ $service = $GLOBALS['cfg']['geo_geocoding_service'];
+ $func = "geo_geocode_{$service}";
+
+ if ((! $service) || (! is_callable($func))){
+
+ return array(
+ 'ok' => 0,
+ 'error' => 'Unknown or undefined service',
+ );
+ }
+
+ $rsp = call_user_func($func, $string);
+
+ $map = geo_geocode_service_map('string keys');
+ $rsp['service_id'] = $map[ $service ];
+
+ return $rsp;
+ }
+
+ #################################################################
+
+ function geo_geocode_yahoo($string){
+
+ $api_key = $GLOBALS['cfg']['geo_geocoding_yahoo_apikey'];
+
+ $url = 'http://where.yahooapis.com/geocode?q='.urlencode($string).'&flags=j&appid='.urlencode($api_key);
+
+ $http_rsp = http_get($url);
+
+ $rsp = array(
+ 'ok' => 0,
+ 'error' => 'unknown error'
+ );
+
+ if ($http_rsp['ok']) {
+
+ # pass in a 1 to disable 'shit-mode'
+ $geocode_response = json_decode($http_rsp['body'], 1);
+
+ if ($geocode_response['ResultSet']['Found'] == 1) {
+
+ $results = $geocode_response['ResultSet']['Results'][0];
+
+ $rsp['ok'] = 1;
+ $rsp['error'] = null;
+ $rsp['latitude'] = (float)$results['latitude'];
+ $rsp['longitude'] = (float)$results['longitude'];
+ $rsp['extras']['woeid'] = (float)$results['woeid'];
+ } else {
+ $rsp['error'] = 'could not geocode';
+ }
+
+ }
+
+ return $rsp;
+ }
+
+?>

0 comments on commit e10c3fe

Please sign in to comment.