You are here

function _get_locations_get_random_places in Get Locations 7

Same name and namespace in other branches
  1. 7.2 modules/getlocations_fields/getlocations_fields.devel_generate.inc \_get_locations_get_random_places()

Get random places.

Parameters

type $quantity the minimum number of items to return:

Return value

array

1 call to _get_locations_get_random_places()
getlocations_fields_devel_generate in modules/getlocations_fields/getlocations_fields.devel_generate.inc
Implements hook_devel_generate().

File

modules/getlocations_fields/getlocations_fields.devel_generate.inc, line 30
getlocations_fields.devel_generate.inc @author Bob Hutchinson https://www.drupal.org/u/hutch @copyright GNU GPL

Code

function _get_locations_get_random_places($quantity = 1) {
  $places = array();
  while ($quantity > count($places)) {
    $qs = array(
      'lat' => rand(0, 90),
      'lng' => rand(0, 180),
      'radius' => 300,
      'username' => variable_get('geonames_webservice_username_parameter', 'demo'),
    );
    $url = 'http://ws.geonames.org/findNearbyPlaceNameJSON?' . http_build_query($qs);
    $result = drupal_http_request($url);
    $data = json_decode($result->data);
    if (!empty($data->status)) {
      drupal_set_message(t('Warning: getlocations random places generator function (%f) failed to use geonames web service; status: "%message" with url @url.', array(
        '%f' => __FUNCTION__,
        '%message' => $data->status->message,
        '@url' => $url,
      )), 'warning');
      break;
    }
    if (!empty($data->geonames)) {
      foreach ($data->geonames as $geoname) {
        $places[] = array(
          'name' => $geoname->name,
          'latitude' => $geoname->lat,
          'longitude' => $geoname->lng,
          'country' => $geoname->countryCode,
        );
      }
    }
    sleep(rand(1, 4));
  }
  return $places;
}