Ejemplo n.º 1
0
        void getWeather(string city, string appid)
        {
            using (WebClient web = new WebClient())
            {
                string url = string.Format("http://api.openweathermap.org/data/2.5/weather?q=" + city + "&appid=" + appid + "&units=metric&cnt=6");

                var json   = web.DownloadString(url);
                var result = JsonConvert.DeserializeObject <WeatherInfo.Root>(json);

                WeatherInfo.Root output = result;


                lbl_citycountry.Content = string.Format("{0}, {1}", output.name, output.sys.country);

                lbl_temp.Content = string.Format("Temperature: {0} \u00B0" + "C", output.main.temp);
                string desc = output.weather[0].description;


                if (desc == "clear sky")
                {
                    weatherImage.Source = new BitmapImage(new Uri("/images/clear sky.png", UriKind.Relative));
                }
                else if (desc == "few clouds")
                {
                    weatherImage.Source = new BitmapImage(new Uri("/images/few clouds.png", UriKind.Relative));
                }
                else if (desc == "scattered clouds")
                {
                    weatherImage.Source = new BitmapImage(new Uri("/images/scattered clouds.png", UriKind.Relative));
                }
                else if (desc == "thunderstorm")
                {
                    weatherImage.Source = new BitmapImage(new Uri("/images/thunderstorm.png", UriKind.Relative));
                }
                else if (desc.Contains("rain") || desc.Contains("drizzle"))
                {
                    weatherImage.Source = new BitmapImage(new Uri("/images/rain.png", UriKind.Relative));
                }
                else if (desc == "broken clouds")
                {
                    weatherImage.Source = new BitmapImage(new Uri("/images/broken clouds.png", UriKind.Relative));
                }
                else if (desc == "mist" || desc == "haze")
                {
                    weatherImage.Source = new BitmapImage(new Uri("/images/mist.png", UriKind.Relative));
                }
                else if (desc == "shower rain")
                {
                    weatherImage.Source = new BitmapImage(new Uri("/images/shower rain.png", UriKind.Relative));
                }
                else if (desc.Contains("snow"))
                {
                    weatherImage.Source = new BitmapImage(new Uri("/images/snow.png", UriKind.Relative));
                }


                lbl_description.Content = string.Format("Weather conditions: {0}", desc);
            }
        }
Ejemplo n.º 2
0
        private void searchEvent(object sender, RoutedEventArgs e)
        {
            var query = txtBox.Text;

            string url = string.Format("http://api.openweathermap.org/data/2.5/weather?q=" + query + "&appid=" + APPID + "&units=metric&cnt=6");

            using (WebClient web = new WebClient())
            {
                try
                {
                    var json   = web.DownloadString(url);
                    var result = JsonConvert.DeserializeObject <WeatherInfo.Root>(json);
                    WeatherInfo.Root output = result;
                    cityName = output.name;

                    getWeather(cityName, APPID);
                    getForecast(cityName, APPID);
                } catch (WebException)
                {
                    MessageBox.Show("City does not exists!");
                }
            }
        }