コード例 #1
0
        public String WeatherDetail(string City)
        {
            //Assign API KEY which received from OPENWEATHERMAP.ORG
            string appId = "b81cd3e18bd5f71679161fd60d7ec325";

            //API path with CITY parameter and other parameters.
            string url = string.Format("http://api.openweathermap.org/data/2.5/weather?q={0}&units=metric&cnt=1&APPID={1}", City, appId);

            using (WebClient client = new WebClient())
            {
                string     json        = client.DownloadString(url);
                RootObject weatherInfo = (new JavaScriptSerializer()).Deserialize <RootObject>(json);

                WeatherModelView rslt = new WeatherModelView();

                rslt.Country       = weatherInfo.sys.country;
                rslt.City          = weatherInfo.name;
                rslt.Lat           = Convert.ToString(weatherInfo.coord.lat);
                rslt.Lon           = Convert.ToString(weatherInfo.coord.lon);
                rslt.Description   = weatherInfo.weather[0].description;
                rslt.Humidity      = Convert.ToString(weatherInfo.main.humidity);
                rslt.Temp          = Convert.ToString(weatherInfo.main.temp);
                rslt.TempFeelsLike = Convert.ToString(weatherInfo.main.feels_like);
                rslt.TempMax       = Convert.ToString(weatherInfo.main.temp_max);
                rslt.TempMin       = Convert.ToString(weatherInfo.main.temp_min);
                rslt.WeatherIcon   = weatherInfo.weather[0].icon;

                //Converting OBJECT to JSON String
                var jsonstring = new JavaScriptSerializer().Serialize(rslt);

                //Return JSON string.
                return(jsonstring);
            }
        }
コード例 #2
0
        public async void GetWeather(LocationWeather location)
        {
            WeatherData weather = await WeatherModelView.GetWeather(location.Lat, location.Lon);

            location.Temperature = weather.main.temp.ToCelsius();
        }