void GetWeather() { StringText sText = new StringText(); sText.GetList(); string cityID = sText.IsEqualCityName(ComboBoxCity.Text); string url = string.Format( "http://api.openweathermap.org/data/2.5/weather?id={0}&units=metric&cnt=6&appid={1}" , cityID, API_KEY); using (WebClient web = new WebClient()) { try { var json = web.DownloadString(url); var result = JsonConvert.DeserializeObject <WeatherInfo.root>(json); WeatherInfo.root output = result; string urlicon = string.Format("http://openweathermap.org/img/wn/{0}@2x.png", output.weather[0].icon); byte[] image = web.DownloadData(urlicon); MemoryStream stream = new MemoryStream(image); Bitmap newbitmap = new Bitmap(stream); Bitmap icon = newbitmap; PictureWeather.Image = newbitmap; labelLog.Text = string.Format("{0}\u00B0", Math.Round(output.coord.lon, 1)); labelLat.Text = string.Format("{0}\u00B0", Math.Round(output.coord.lat, 1)); labelTemp.Text = string.Format("{0}\u00B0" + "C", Math.Round(output.main.temp, 0)); if (Math.Round(output.main.temp_min, 0) == Math.Round(output.main.temp_max, 0)) { labelTempMinMax.Text = string.Format("{0}\u00B0" + "C", Math.Round(output.main.temp_max, 0)); } else { labelTempMinMax.Text = string.Format("{0}\u00B0" + "C/{1}\u00B0" + "C", Math.Round(output.main.temp_min, 0), Math.Round(output.main.temp_max, 0)); } labelWeather.Text = sText.IsEqualMainWeather(output.weather[0].main); labelCloud.Text = sText.IsEqualDescriptionWeather(output.weather[0].description); labelHumidity.Text = string.Format("{0}%", output.main.humidity); labelWind.Text = string.Format("{0} km/h", output.wind.speed); labelPressure.Text = string.Format("{0} hPa", output.main.pressure); labelVisible.Text = string.Format("{0} km", output.visibility / 1000); labelDate.Text = string.Format("NGÀY {0}/{1}/{2}" , getDate(output.dt).Day, getDate(output.dt).Month, getDate(output.dt).Year); labelHour.Text = string.Format("Giờ {0}", getDate(output.dt).ToShortTimeString()); labelWeather.Location = new Point((PanelDisplayWT.Size.Width / 2) - (labelWeather.Size.Width / 2), 0); labelTemp.Location = new Point((PanelDisplayWT.Size.Width / 2) - (labelTemp.Size.Width / 2), 54); labelHour.Text = DateTime.Now.ToString("t"); } catch (System.Net.WebException) { PanelDisplayWT.Visible = false; MessageBox.Show("Vui lòng kết nối mạng trước khi sử dụng tính năng này.", "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } }
void getWeather(string city) { using (WebClient web = new WebClient()) { string url = string.Format("http://api.openweathermap.org/data/2.5/weather?q={0}&appid=c673e9ca6ed3280af36b5fd5888246e2&units=metric&cnt=6", city); var json = web.DownloadString(url); var result = JsonConvert.DeserializeObject <WeatherInfo.root>(json); WeatherInfo.root output = result; textBoxCityName.Text = string.Format("{0}", output.name); textBoxCountry.Text = string.Format("{0}", output.sys.country); textBoxTemp.Text = string.Format("{0} \u00B0" + "C", output.main.temp); } }
// Returns the weather and description given the city and time private string GetWeather(string location, string time) { const string apiKey = ""; string url = $"http://api.openweathermap.org/data/2.5/weather?q={location}&appid={apiKey}&units=metric&cnt=6"; var json = new WebClient().DownloadString(url); var jsonResult = JsonConvert.DeserializeObject <WeatherInfo.root>(json); WeatherInfo.root output = jsonResult; string result = $"{output.main.temp}�C with {output.weather[0].description}"; return(result); }
public ActionResult Meteo(double?lat, double?lon) { using (WebClient web = new WebClient()) { CoordonnerViewModel coordonner = new CoordonnerViewModel(); string url = string.Format("http://api.openweathermap.org/data/2.5/weather?lat={0}&lon={1}&APPID={2}&units=metric&cnt=6", lat, lon, APPID); var json = web.DownloadString(url); var result = JsonConvert.DeserializeObject <WeatherInfo.root>(json); WeatherInfo.root outPut = result; coordonner.nomPays = outPut.name + ", " + outPut.sys.country; // cityName = txt_nom_ville.Text; // txt_date_jour.Text = WeatherInfo.Jour() + ", le " + DateTime.Now.Day.ToString() + " " + weatherInfo.MoisEnFrancais() + " " + DateTime.Now.Year.ToString(); coordonner.Temperature = string.Format(outPut.main.temp.ToString() + "\u00B0" + " C"); // meteo = txt_Temperature.Text; coordonner.Precipitation = outPut.main.humidity.ToString() + "% de précipitation"; return(PartialView("Mete", coordonner)); } }
//string CityName = "London"; void getWeather(String CityName) { using (WebClient web = new WebClient()) { string url = string.Format("https://openweathermap.org/data/2.5/weather?q={0}&appid={1}", CityName, APIID); var json = web.DownloadString(url); var result = JsonConvert.DeserializeObject <WeatherInfo.root>(json); WeatherInfo.root outPut = result; // Outputs weather API to labels on screen // lblCityName.Text = string.Format("{0}", outPut.name); lblCountry.Text = string.Format("Country: " + "{0}", outPut.sys.country); lblTemp.Text = string.Format("Current Temperature: " + "{0} \u00B0" + "C", outPut.main.temp); lblHumididty.Text = string.Format("Humidity: " + "{0}" + "%", outPut.main.humidity); lblWindSpeed.Text = string.Format("Wind Speed: " + "{0}" + "MPH", outPut.wind.speed); lblMinTemp.Text = string.Format("Min Temperature: " + "{0} \u00B0" + "C", outPut.main.temp_min); lblMaxTemp.Text = string.Format("Max Temperature: " + "{0} \u00B0" + "C", outPut.main.temp_max); lblLocation.Visible = true; } }
public ActionResult Index() { #region Weather #region Current string Miasto = "Limanowa"; if (!Request["Wybrane_miasto"].IsEmpty()) { Miasto = Request["Wybrane_miasto"]; } weatherInfo.WeatherInfo.root checkWeather = new WeatherInfo.root(); weather weather = new weather(); checkWeather = weather.DownloadWeather(Miasto); bool sprawdz = true; if (checkWeather != null) { sprawdz = true; // lastweather.SaveWeather(checkWeather); ViewBag.weatherName = checkWeather.name; ViewBag.weatherCountry = checkWeather.sys.country; ViewBag.weatherTemp = checkWeather.main.temp.ToString(); ViewBag.weatherfeelsLike = checkWeather.main.feels_like.ToString(); ViewBag.weatherTempMax = checkWeather.main.temp_max.ToString(); ViewBag.weatherTempMin = checkWeather.main.temp_min.ToString(); ViewBag.weatherPressure = checkWeather.main.pressure.ToString(); ViewBag.weatherHumidity = checkWeather.main.humidity.ToString(); ViewBag.weatherWindSpeed = checkWeather.wind.speed.ToString(); } else { sprawdz = false; ViewBag.error = "Brak miasta " + Miasto + " w bazie danych"; } #endregion #region Historia weatherInfo.HistoriWeatherInfo.root yeasterdayweater = new weatherInfo.HistoriWeatherInfo.root(); PastWeather pastWeather = new PastWeather(); UnixTime wczorajTime = new UnixTime(); int time = wczorajTime.WczorajUnixTime(); int dtNow = 15; if (sprawdz == true) { yeasterdayweater = pastWeather.DownloandLastWeather(checkWeather.coord.lat, checkWeather.coord.lon, time); //dtNow = pastWeather.ActualTime(yeasterdayweater); } if (yeasterdayweater.hourly != null && dtNow != -1) { ViewBag.yeasterdayWeaterTemp = yeasterdayweater.hourly[dtNow].temp; if (checkWeather.main.temp > yeasterdayweater.hourly[dtNow].temp) { int one = (int)((checkWeather.main.temp - yeasterdayweater.hourly[dtNow].temp) * 100); double two = (double)one / 100; ViewBag.yeasterdayDifferenceTempInt = two; ViewBag.yeasterdayDifferenceTempString = "cieplej "; } else if (checkWeather.main.temp < yeasterdayweater.hourly[dtNow].temp) { int one = (int)((yeasterdayweater.hourly[dtNow].temp - checkWeather.main.temp) * 100); double two = (double)one / 100; ViewBag.yeasterdayDifferenceTempInt = two; ViewBag.yeasterdayDifferenceTempString = "zimniej "; } ViewBag.yeasterdayWeaterfeelslike = yeasterdayweater.hourly[dtNow].feels_like; if (checkWeather.main.feels_like > yeasterdayweater.hourly[dtNow].feels_like) { int one = (int)((checkWeather.main.feels_like - yeasterdayweater.hourly[dtNow].feels_like) * 100); double two = (double)one / 100; ViewBag.yeasterdayDifferencefeelslikeInt = two; ViewBag.yeasterdayDifferencefeelslikeString = "cieplej "; } else if (checkWeather.main.feels_like < yeasterdayweater.hourly[dtNow].feels_like) { int one = (int)((yeasterdayweater.hourly[dtNow].feels_like - checkWeather.main.feels_like) * 100); double two = (double)one / 100; ViewBag.yeasterdayDifferencefeelslikeint = two; ViewBag.yeasterdayDifferencefeelslikeString = "zimniej "; } ViewBag.yeasterdayWeaterPressure = yeasterdayweater.hourly[dtNow].pressure; if (checkWeather.main.pressure > yeasterdayweater.hourly[dtNow].pressure) { ViewBag.yeasterdayDifferencePressureInt = checkWeather.main.pressure - yeasterdayweater.hourly[dtNow].pressure; ViewBag.yeasterdayDifferencePressureString = "wyższe"; } else if (checkWeather.main.pressure < yeasterdayweater.hourly[dtNow].pressure) { ViewBag.yeasterdayDifferencePressureInt = yeasterdayweater.hourly[dtNow].pressure - checkWeather.main.pressure; ViewBag.yeasterdayDifferencePressureString = "niższe"; } ViewBag.yeasterdayWeaterHumidity = yeasterdayweater.hourly[dtNow].humidity; if (checkWeather.main.humidity > yeasterdayweater.hourly[dtNow].humidity) { ViewBag.yeasterdayDifferenceHumidityInt = checkWeather.main.humidity - yeasterdayweater.hourly[dtNow].humidity; ViewBag.yeasterdayDifferenceHumidityString = "wyższe"; } else if (checkWeather.main.humidity < yeasterdayweater.hourly[dtNow].humidity) { ViewBag.yeasterdayDifferenceHumidityInt = yeasterdayweater.hourly[dtNow].humidity - checkWeather.main.humidity; ViewBag.yeasterdayDifferenceHumidityString = "niższe"; } ViewBag.yeasterdayWeaterWeaterWindSpeed = yeasterdayweater.hourly[dtNow].wind_speed; if (checkWeather.wind.speed > yeasterdayweater.hourly[dtNow].wind_speed) { ViewBag.yeasterdayDifferenceWindSpeedInt = checkWeather.wind.speed - yeasterdayweater.hourly[dtNow].wind_speed; ViewBag.yeasterdayDifferenceWindSpeedString = "mocniejszy"; } else if (checkWeather.wind.speed < yeasterdayweater.hourly[dtNow].wind_speed) { ViewBag.yeasterdayDifferenceWindSpeedInt = yeasterdayweater.hourly[dtNow].wind_speed - checkWeather.wind.speed; ViewBag.yeasterdayDifferenceWindSpeedString = "słabszy"; } } else { ViewBag.lastError = "Nie udało się pobrać danych wczorajszego dnia"; } #endregion #region Prognoza FutureWeatherInfo.root futureWeather = new FutureWeatherInfo.root(); FutureWeather dowloand = new FutureWeather(); UnixTime unixTime = new UnixTime(); if (sprawdz == true) { futureWeather = dowloand.DownloandFutureWeather(checkWeather.coord.lat, checkWeather.coord.lon); Nazwa_Miasta = checkWeather.name; publicLat = checkWeather.coord.lat; publicLon = checkWeather.coord.lon; } if (futureWeather.daily != null) { ViewBag.futureWeatherTemp = futureWeather.daily[0].temp.day; if (checkWeather.main.temp > futureWeather.daily[0].temp.day) { int one = (int)((checkWeather.main.temp - futureWeather.daily[0].temp.day) * 100); double two = (double)one / 100; ViewBag.futureDifferenceTempInt = two; ViewBag.futureDifferenceTempString = "zimniej "; } else if (checkWeather.main.temp < futureWeather.daily[0].temp.day) { int one = (int)((futureWeather.daily[0].temp.day - checkWeather.main.temp) * 100); double two = (double)one / 100; ViewBag.futureDifferenceTempInt = two; ViewBag.futureDifferenceTempString = "cieplej "; } ViewBag.futureWeatherTempMax = futureWeather.daily[0].temp.max; if (checkWeather.main.temp_max > futureWeather.daily[0].temp.max) { int one = (int)((checkWeather.main.temp_max - futureWeather.daily[0].temp.max) * 100); double two = (double)one / 100; ViewBag.futureDifferenceTempMaxInt = two; ViewBag.futureDifferenceTempMaxString = "zimniej"; } else if (checkWeather.main.temp_max < futureWeather.daily[0].temp.max) { int one = (int)((futureWeather.daily[0].temp.max - checkWeather.main.temp_max) * 100); double two = (double)one / 100; ViewBag.futureDifferenceTempMaxInt = two; ViewBag.futureDifferenceTempMaxString = "cieplej"; } ViewBag.futureWeatherTempMin = futureWeather.daily[0].temp.min; if (checkWeather.main.temp_min > futureWeather.daily[0].temp.min) { int one = (int)((checkWeather.main.temp_min - futureWeather.daily[0].temp.min) * 100); double two = (double)one / 100; ViewBag.futureDifferenceTempMinInt = two; ViewBag.futureDifferenceTempMinString = "zimniej "; } else if (checkWeather.main.temp_min > futureWeather.daily[0].temp.min) { int one = (int)((futureWeather.daily[0].temp.min - checkWeather.main.temp_min) * 100); double two = (double)one / 100; ViewBag.futureDifferenceTempMinInt = two; ViewBag.futureDifferenceTempMinString = "cieplej "; } ViewBag.futureWeatherFeelsLike = futureWeather.daily[0].feels_Like.day; if (checkWeather.main.feels_like > futureWeather.daily[0].feels_Like.day) { int one = (int)((checkWeather.main.feels_like - futureWeather.daily[0].feels_Like.day) * 100); double two = (double)one / 100; ViewBag.futureDifferenceFeelsLikeInt = two; ViewBag.futureDifferenceFeelsLikeString = "zimniej "; } else if (checkWeather.main.feels_like < futureWeather.daily[0].feels_Like.day) { int one = (int)((futureWeather.daily[0].feels_Like.day - checkWeather.main.feels_like) * 100); double two = (double)one / 100; ViewBag.futureDifferenceFeelsLikeInt = two; ViewBag.futureDifferenceFeelsLikeString = "cieplej "; } ViewBag.futureWeatherPressure = futureWeather.daily[0].pressure; if (checkWeather.main.pressure > futureWeather.daily[0].pressure) { int one = (int)((checkWeather.main.pressure - futureWeather.daily[0].pressure) * 100); double two = (double)one / 100; ViewBag.futureDifferencePressureInt = two; ViewBag.futureDifferencePressureString = "niższe "; } else if (checkWeather.main.pressure < futureWeather.daily[0].pressure) { int one = (int)((futureWeather.daily[0].pressure - checkWeather.main.pressure) * 100); double two = (double)one / 100; ViewBag.futureDifferencePressureInt = two; ViewBag.futureDifferencePressureString = "wyższe "; } ViewBag.futureWeatherHumidity = futureWeather.daily[0].humidity; if (checkWeather.main.humidity > futureWeather.daily[0].humidity) { int one = (int)((checkWeather.main.humidity - futureWeather.daily[0].humidity) * 100); double two = (double)one / 100; ViewBag.futureDifferenceHumidityInt = two; ViewBag.futureDifferenceHumidityString = "niższe "; } else if (checkWeather.main.humidity < futureWeather.daily[0].humidity) { int one = (int)((futureWeather.daily[0].humidity - checkWeather.main.humidity) * 100); double two = (double)one / 100; ViewBag.futureDifferenceHumidityInt = two; ViewBag.futureDifferenceHumidityString = "wyższe "; } ViewBag.futureWeatherWindSpeed = futureWeather.daily[0].wind_speed; if (checkWeather.wind.speed > futureWeather.daily[0].wind_speed) { int one = (int)((checkWeather.wind.speed - futureWeather.daily[0].wind_speed) * 100); double two = (double)one / 100; ViewBag.futureDifferenceWindSpeedInt = two; ViewBag.futureDifferenceWindSpeedString = "słabszy "; } else if (checkWeather.wind.speed < futureWeather.daily[0].wind_speed) { int one = (int)((futureWeather.daily[0].wind_speed - checkWeather.wind.speed) * 100); double two = (double)one / 100; ViewBag.futureDifferenceWindSpeedInt = two; ViewBag.futureDifferenceWindSpeedString = "mocniejszy "; } }//szegóły jutrzejszego dnia else { ViewBag.FutureError = "Nie udało się pobrać prognozowanej pogody"; } if (futureWeather.daily != null) { ViewBag.weatherLon = checkWeather.coord.lon; ViewBag.weatherLat = checkWeather.coord.lat; string dzień = unixTime.AktualnyDzieńString(futureWeather.daily[0].dt); ViewBag.futureWeatherOneDay = dzień; ViewBag.futureWeatherOneTemp = futureWeather.daily[0].temp.day; ViewBag.futureWeatherOnePop = futureWeather.daily[0].pop * 100; dzień = unixTime.AktualnyDzieńString(futureWeather.daily[1].dt); ViewBag.futureWeatherTwoDay = dzień; ViewBag.futureWeatherTwoTemp = futureWeather.daily[1].temp.day; ViewBag.futureWeatherTwoPop = futureWeather.daily[1].pop * 100; dzień = unixTime.AktualnyDzieńString(futureWeather.daily[2].dt); ViewBag.futureWeatherThreeDay = dzień; ViewBag.futureWeatherThreeTemp = futureWeather.daily[2].temp.day; ViewBag.futureWeatherThreePop = futureWeather.daily[2].pop * 100; dzień = unixTime.AktualnyDzieńString(futureWeather.daily[3].dt); ViewBag.futureWeatherFourDay = dzień; ViewBag.futureWeatherFourTemp = futureWeather.daily[3].temp.day; ViewBag.futureWeatherFourPop = futureWeather.daily[3].pop * 100; dzień = unixTime.AktualnyDzieńString(futureWeather.daily[4].dt); ViewBag.futureWeatherFiveDay = dzień; ViewBag.futureWeatherFiveTemp = futureWeather.daily[4].temp.day; ViewBag.futureWeatherFivePop = futureWeather.daily[4].pop * 100; dzień = unixTime.AktualnyDzieńString(futureWeather.daily[5].dt); ViewBag.futureWeatherSixDay = dzień; ViewBag.futureWeatherSixTemp = futureWeather.daily[5].temp.day; ViewBag.futureWeatherSixPop = futureWeather.daily[5].pop * 100; dzień = unixTime.AktualnyDzieńString(futureWeather.daily[6].dt); ViewBag.futureWeatherSevenDay = dzień; ViewBag.futureWeatherSevenTemp = futureWeather.daily[6].temp.day; ViewBag.futureWeatherSevenPop = futureWeather.daily[6].pop * 100; dzień = unixTime.AktualnyDzieńString(futureWeather.daily[7].dt); ViewBag.futureWeatherEightDay = dzień; ViewBag.futureWeatherEightTemp = futureWeather.daily[7].temp.day; ViewBag.futureWeatherEightPop = futureWeather.daily[7].pop * 100; } //Szybka probnoza na 8 dni #endregion #endregion return(View()); }