示例#1
0
 /**
  * Geocoding function that uses nominatin engine.
  *
  * @param 	string 				Address
  * @return 	string[]|boolean	Location information [country, country_id, location_name, latitude, longitude], FALSE if unsucessful
  *
  */
 public static function nominatim($address)
 {
     $payload = FALSE;
     $result = FALSE;
     $params = array("format" => "json", "addressdetails" => 1, "accept-language" => Settings_Model::get('site_language'), "q" => $address, "zoom" => 200);
     $url = "http://nominatim.openstreetmap.org/search/?" . http_build_query($params);
     $url_request = new HttpClient($url);
     if ($result = $url_request->execute()) {
         $payload = json_decode($result);
     } else {
         Kohana::log('error', "Geocode - Nominatin\n" . $url_request->get_error_msg());
     }
     // TODO: Nomaninatins documentation on error returning is poor - this could be improved to have meaningful error messages
     if (!$payload || count($payload) == 0) {
         return FALSE;
     }
     $result = array_shift($payload);
     $country_name = isset($result->address->country) ? $result->address->country : $result->display_name;
     $country = self::getCountryId($country_name);
     // if we can't find the country by name, try finding it by code
     if ($country == 0 && isset($result->address->country_code)) {
         $country = self::getCountryIdByCode($result->address->country_code);
     }
     $geocodes = array('country' => $country_name, 'country_id' => $country, 'location_name' => $result->display_name, 'latitude' => $result->lat, 'longitude' => $result->lon);
     return $geocodes;
 }