コード例 #1
0
 /**
  * Add missing location fields, and update country and admin codes with
  * authoritative Geonames values.
  *
  * @since 1.3
  *
  * @param array $location The location to geocode, modified.
  * @param string $language Optional ISO language code.
  * @return bool Success.
  */
 private static function reverse_geocode_location(&$location, $language = '')
 {
     global $geo_mashup_options;
     // Coordinates are required
     if (self::are_any_location_fields_empty($location, array('lat', 'lng'))) {
         return false;
     }
     // Don't bother unless there are missing geocodable fields
     $geocodable_fields = array('country_code', 'admin_code', 'address', 'locality_name', 'postal_code');
     $have_empties = self::are_any_location_fields_empty($location, $geocodable_fields);
     if (!$have_empties) {
         return false;
     }
     $status = false;
     if (!class_exists('GeoMashupHttpGeocoder')) {
         include_once path_join(GEO_MASHUP_DIR_PATH, 'geo-mashup-geocoders.php');
     }
     $geonames_geocoder = new GeoMashupGeonamesGeocoder();
     $geonames_results = $geonames_geocoder->reverse_geocode($location['lat'], $location['lng']);
     if (is_wp_error($geonames_results) or empty($geonames_results)) {
         self::$geocode_error = $geonames_results;
     } else {
         if (!empty($geonames_results[0]->admin_code)) {
             $location['admin_code'] = $geonames_results[0]->admin_code;
         }
         if (!empty($geonames_results[0]->country_code)) {
             $location['country_code'] = $geonames_results[0]->country_code;
         }
         self::fill_empty_location_fields($location, (array) $geonames_results[0]);
         $status = true;
     }
     $have_empties = self::are_any_location_fields_empty($location, $geocodable_fields);
     if ($have_empties) {
         // Choose a geocoding service based on the default API in use
         if ('google' == substr($geo_mashup_options->get('overall', 'map_api'), 0, 6)) {
             $next_geocoder = new GeoMashupGoogleGeocoder();
         } else {
             if ('openlayers' == $geo_mashup_options->get('overall', 'map_api')) {
                 $next_geocoder = new GeoMashupNominatimGeocoder();
             }
         }
         $next_results = $next_geocoder->reverse_geocode($location['lat'], $location['lng']);
         if (is_wp_error($next_results) or empty($next_results)) {
             self::$geocode_error = $next_results;
         } else {
             self::fill_empty_location_fields($location, (array) $next_results[0]);
         }
         $status = true;
     }
     return $status;
 }
コード例 #2
0
 /**
  * Add missing location fields, and update country and admin codes with
  * authoritative Geonames values.
  *
  * @since 1.3
  *
  * @param array $location The location to geocode, modified.
  * @param string $language Optional ISO language code.
  * @return bool Success.
  */
 private static function reverse_geocode_location(&$location, $language = '')
 {
     // Coordinates are required
     if (self::are_any_location_fields_empty($location, array('lat', 'lng'))) {
         return false;
     }
     // Don't bother unless there are missing geocodable fields
     $geocodable_fields = array('country_code', 'admin_code', 'address', 'locality_name', 'postal_code');
     $have_empties = self::are_any_location_fields_empty($location, $geocodable_fields);
     if (!$have_empties) {
         return false;
     }
     $status = false;
     if (!class_exists('GeoMashupHttpGeocoder')) {
         include_once path_join(GEO_MASHUP_DIR_PATH, 'geo-mashup-geocoders.php');
     }
     $geonames_geocoder = new GeoMashupGeonamesGeocoder();
     $geonames_results = $geonames_geocoder->reverse_geocode($location['lat'], $location['lng']);
     if (is_wp_error($geonames_results) or empty($geonames_results)) {
         self::$geocode_error = $geonames_results;
     } else {
         if (!empty($geonames_results[0]->admin_code)) {
             $location['admin_code'] = $geonames_results[0]->admin_code;
         }
         if (!empty($geonames_results[0]->country_code)) {
             $location['country_code'] = $geonames_results[0]->country_code;
         }
         self::fill_empty_location_fields($location, (array) $geonames_results[0]);
         $status = true;
     }
     $have_empties = self::are_any_location_fields_empty($location, $geocodable_fields);
     $alternate_geocoder = self::make_alternate_reverse_geocoder();
     if ($have_empties and $alternate_geocoder) {
         $next_results = $alternate_geocoder->reverse_geocode($location['lat'], $location['lng']);
         if (is_wp_error($next_results) or empty($next_results)) {
             self::$geocode_error = $next_results;
         } else {
             self::fill_empty_location_fields($location, (array) $next_results[0]);
         }
         $status = true;
     }
     return $status;
 }