public async Task <Dictionary <string, string> > GetCurrentWeather(Position currentPosition) { // API call parameters string apiKey = "appid=" + Constants.WEATHER_API_KEY; string url = "http://api.openweathermap.org/data/2.5/weather?"; string pos = "lat=" + currentPosition.Latitude + "&lon=" + currentPosition.Longitude; string unit = "units=metric"; // Httpclient HttpClient client = new HttpClient(); string test = String.Format("{0}{1}&{2}&{3}", url, pos, unit, apiKey); // Get Json string x = await client.GetStringAsync(new Uri(String.Format("{0}{1}&{2}&{3}", url, pos, unit, apiKey))); // Deserialize CurrentWeather.RootObject rootObject = JsonConvert.DeserializeObject <CurrentWeather.RootObject>(x); // Analyze all elements in Json data string cityName = rootObject.name; string temp = rootObject.main.temp + "°C"; string icon = rootObject.weather[0].icon; // text Dictionary <string, string> dic = new Dictionary <string, string>() { { "CityAndTemp", $"{cityName} {temp}" }, { "IconURL", "http://openweathermap.org/img/w/" + icon + ".png" } }; return(dic); }
public void MapCurrentWeatherData(string city) { // This URI will retrieved the current conditions string JSONstring = getString("http://api.wunderground.com/api/6c99744103fecd5c/conditions/q/TX/" + city + ".json"); if (JSONstring != "Error") { CurrentWeather.RootObject CurrentObj = JsonConvert.DeserializeObject <CurrentWeather.RootObject>(JSONstring); DayLabel.Text = CurrentObj.current_observation.observation_time_rfc822; TempLabel.Text = CurrentObj.current_observation.temp_f.ToString(); HeatIndexLabel.Text = CurrentObj.current_observation.heat_index_f.ToString(); ClimateLabel.Text = CurrentObj.current_observation.feelslike_f.ToString(); WindLabel.Text = CurrentObj.current_observation.wind_gust_mph.ToString(); DewLabel.Text = CurrentObj.current_observation.dewpoint_f.ToString(); HumidityLabel.Text = CurrentObj.current_observation.relative_humidity.ToString(); } else { ErrorLabel.Text = "Error connecting to wunderground Server, try again later"; ErrorLabel.Visible = true; } }