You are here

function wunderground_weather_location_autocomplete in Wunderground weather 7

Autocomplete function to get locations from the Wunderground database.

Parameters

string $text: Input from the autocomplete widget.

1 string reference to 'wunderground_weather_location_autocomplete'
wunderground_weather_menu in ./wunderground_weather.module
Implements hook_menu().

File

./wunderground_weather.module, line 745
Wunderground weather module to display weather forecasts and current weather conditions in blocks.

Code

function wunderground_weather_location_autocomplete($text = '') {
  $query = urlencode($text);
  $request_url = 'http://autocomplete.wunderground.com/aq?query=' . $query;

  // Get data from wunderground.
  $response = drupal_http_request($request_url);
  if ($response->code == 200) {
    $data = drupal_json_decode($response->data);
  }

  // Extract key and value from the returned array.
  $results = array();
  foreach ($data['RESULTS'] as $result) {
    if (!isset($result['l']) && isset($result['ll'])) {
      $location = str_replace(' ', ',', $result['ll']);
      $geolookup_response = wunderground_weather_geolookup($location);
      $geolookup_data = drupal_json_decode($geolookup_response->data);
      if (isset($geolookup_data['error'])) {
        continue;
      }
      else {
        $result['l'] = $geolookup_data['location']['l'];
      }
    }
    $results[$result['name'] . ' [' . $result['l'] . ']'] = $result['name'];
  }

  // Return as json.
  drupal_json_output($results);
}