override public async Task <ZmiennePogoda> OtrzymajPogodę(string miasto, string kraj) { // Darmowy klucz dostępu oraz zapytanie do strony string klucz = "f8dc63a23acc6e0e69070a66a3c01c0a"; string tekstZapytania = "http://api.openweathermap.org/data/2.5/weather?q=" + miasto + "," + kraj + "&appid=" + klucz + "&units=metric"; dynamic resultat = await PobieraniePogody.otrzymajDane(tekstZapytania).ConfigureAwait(false); // Jeżeli wartości pogody są różne od null to stwórz obiekt i przypisz mu odpowiednie pobrane wartości if (resultat["weather"] != null) { //Pobranie odpowiednich wartości zmiennych i dodanie im odpowiedniej jednostki ZmiennePogoda pogoda = new ZmiennePogoda(); pogoda.Tytuł = (string)resultat["name"]; pogoda.Temperatura = (string)resultat["main"]["temp"] + " °C"; pogoda.Wiatr = (string)resultat["wind"]["speed"] + " m/s"; pogoda.Wilgotność = (string)resultat["main"]["humidity"] + " %"; pogoda.Widoczność = (string)resultat["weather"][0]["main"]; //Operacje nad czasem,pobranie czasu wschodu i zachodu słońca DateTime czas = new System.DateTime(1970, 1, 1, 0, 0, 0, 0); DateTime WschódSłońca = czas.AddSeconds((double)resultat["sys"]["sunrise"]); DateTime ZachódSłońca = czas.AddSeconds((double)resultat["sys"]["sunset"]); pogoda.WschódSłońca = WschódSłońca.ToString() + " UTC"; pogoda.ZachódSłońca = ZachódSłońca.ToString() + " UTC"; return(pogoda); } else { return(null); } }
private async void KliknieciePrzycisku(object sender, EventArgs e) { //Pobieranie po kliknieciu dla miasta danych pogodowych //Sprawdzenie przy tym czy klient wpisał dane string tekst1; tekst1 = tekstMiasto.Text; string tekst2; tekst2 = tekstPaństwo.Text; try { UzyskiwanieWartości uzyskano = new UzyskiwanieWartości(); ZmiennePogoda pogoda = await uzyskano.OtrzymajPogodę(tekst1, tekst2); switch (pogoda.Widoczność) { // Pobranie w zależności od warunków pogodowych odpowiedniego zdjęcia case "Rain": pogoda.Widoczność = "deszczowo"; Obrazek.Load("http://openweathermap.org/img/w/10d.png"); break; case "Clouds": pogoda.Widoczność = "z chmurami"; Obrazek.Load("http://openweathermap.org/img/w/02d.png"); break; case "Clear": pogoda.Widoczność = "bez chmurnie"; Obrazek.Load("http://openweathermap.org/img/w/01d.png"); break; case "Mist": pogoda.Widoczność = "mglisto"; Obrazek.Load("http://openweathermap.org/img/w/50d.png"); break; case "Haze": pogoda.Widoczność = "mglisto"; Obrazek.Load("http://openweathermap.org/img/w/50d.png"); break; case "Snow": pogoda.Widoczność = "śnieżnie"; Obrazek.Load("http://openweathermap.org/img/w/13d.png"); break; } TytułTekst.Text = pogoda.Tytuł; TemperaturaTekst.Text = pogoda.Temperatura; WiatrTekst.Text = pogoda.Wiatr; WilgotnośćTekst.Text = pogoda.Wilgotność; WidocznośćTekst.Text = pogoda.Widoczność; WschódTekst.Text = pogoda.WschódSłońca; ZachódTekst.Text = pogoda.ZachódSłońca; UstawWartość = TytułTekst.Text; } catch { MessageBox.Show("Nie wprowadzano danych!", "Błąd pobierania", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); } }