diff --git a/wunderground_weather.module b/wunderground_weather.module index caeff6e..e988e6a 100644 --- a/wunderground_weather.module +++ b/wunderground_weather.module @@ -689,6 +689,18 @@ function wunderground_weather_location_autocomplete($text = '') { // 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. @@ -730,6 +742,28 @@ function wunderground_weather_current_fields() { } /** + * @param $location + * Location to get geolookup data for. + * + * @return bool|object|\stdClass + * Response from Wunderground Weather geolookup. + * + * @see http://www.wunderground.com/weather/api/d/docs?d=data/geolookup + */ +function wunderground_weather_geolookup($location) { + $response = FALSE; + $request_url = 'http://api.wunderground.com/api'; + $api_key = variable_get('wunderground_weather_api_key'); + + if ($api_key) { + $request_url .= '/' . $api_key . '/geolookup/q/' . $location . '.json'; + $response = drupal_http_request($request_url); + } + + return $response; +} + +/** * * Transform the url to user an other icon set. *