string getBackground(APICurrentWeather currentWeather) { string desc = currentWeather.weather[0].description; if (desc.Contains("cloud") || desc.Contains("rain") || desc.Contains("shower") || desc.Contains("storm") || desc.Contains("drizzle")) { return("cloudy.jpg"); } int randomIndex = new System.Random().Next(2); return(new string[] { "sunny.png", "skyWithClouds.jpg" }[randomIndex]); }
void GetAPIWeather(int id) { APICurrentWeather currentWeather = Helpers.APICurrentWeather.GetCurrentWeather(id + ""); TempData["HasForecast"] = true; TempData["City"] = currentWeather.name + ", " + currentWeather.sys.country; TempData["Desc"] = currentWeather.weather[0].main + " - " + currentWeather.weather[0].description; TempData["Min"] = Math.Round(currentWeather.main.temp_min) + " °C"; TempData["Max"] = Math.Round(currentWeather.main.temp_max) + " °C"; TempData["Date"] = DateTime.Now.Date.ToLongDateString(); TempData["Wind"] = currentWeather.wind.speed + " km /h"; TempData["Humidity"] = currentWeather.main.humidity + " % "; TempData["Precip"] = currentWeather.GetRain() + " mm in last 3 hours"; TempData["Background"] = getBackground(currentWeather); }
//Auto fill form with data from API private void BtnAutofill_Click(object sender, RoutedEventArgs e) { if (!checkValidInputsAutofill()) { return; } crdError.Visibility = Visibility.Hidden; APICurrentWeather currentWeather = APICurrentWeather.GetCurrentWeather(((City)lstCities.SelectedItem).id + ""); dtpDate.SelectedDate = DateTime.Now; sldMin.Value = currentWeather.main.temp_min; sldMax.Value = currentWeather.main.temp_max; sldWind.Value = currentWeather.wind.speed; sldHumidity.Value = currentWeather.main.humidity; sldPrecip.Value = currentWeather.GetRain(); }
private void showForecast(string id) { //Using OpenWeatherMap API, get current weather in city with specified ID APICurrentWeather currentWeather = APICurrentWeather.GetCurrentWeather(id); lblCity.Text = currentWeather.name + ", " + currentWeather.sys.country; lblMin.Text = Math.Round(currentWeather.main.temp_min) + " °C"; lblMax.Text = Math.Round(currentWeather.main.temp_max) + " °C"; lblDesc.Text = currentWeather.weather[0].main + " - " + currentWeather.weather[0].description; lblDate.Text = DateTime.Now.Date.ToLongDateString(); lblWindSpeed.Text = currentWeather.wind.speed + " km/h"; lblHumidity.Text = currentWeather.main.humidity + " %"; lblPrecipitation.Text = currentWeather.GetRain() + " mm in last 3 hours"; //Set background image based on weather string src = ""; if (cloudy(currentWeather)) { src = "cloudy.jpg"; } else { src = "sunny.jpg"; } ImageBrush backgroundBrush = new ImageBrush(); Image image = new Image(); image.Source = new BitmapImage(new Uri(Directory.GetCurrentDirectory() + "/BackgroundImages/" + src)); backgroundBrush.ImageSource = image.Source; grdMain.Background = backgroundBrush; backgroundBrush.Opacity = 0.3; this.UpdateLayout(); }
//Decides if the weather report says anything about clouds private bool cloudy(APICurrentWeather currentWeather) { string desc = currentWeather.weather[0].description; return(desc.Contains("cloud") || desc.Contains("rain") || desc.Contains("shower") || desc.Contains("storm") || desc.Contains("drizzle")); }