Recently we published a library that allows you to get weather data based on geo location. This location can be co-ordinates or name of city etc. This library is built upon OpenWeatherMap web services. Here are some code snippets that show you two services that are provided in our library.
OpenWeatherMap web service is free to use. The publishers of this service do not force you to register. But they do recommend that you get an application id to get more accurate and guaranteed results from service.
You can visit OpenWeatherMap to get started. Once you have application id, store it safely from your application can access it.
This task is as simple as calling GetCurrentWeather method. Following code snippet shows the example.
var serviceConfigutation = new WeatherServiceConfiguration() { AppKey = "" }; var service = new WeatherService(serviceConfigutation); var weather = service.GetCurrentWeather("Boston","","USA"); if (weather.StatusCode == 200) { Console.WriteLine(weather.CurrentCondition.Temperature); }
The second parameter for this method is not used at the moment. It is placeholder to provide state or region information.
If you have exact coordinates of location, you can invoke the method using those values.
var configuration = new WeatherServiceConfiguration() { AppKey = ConfigurationManager.AppSettings["openweather_key"] }; var weatherService = new WeatherService(configuration); var weather = weatherService.GetForecastWeather(this.GeoLocation.Longitude.Value, this.GeoLocation.Latitude.Value);
You can call GetForecastWeather method. It has exactly the same overloads as GetCurrentWeather method. The only difference is the return object.
var serviceConfigutation = new WeatherServiceConfiguration() { AppKey = "" }; var service = new WeatherService(serviceConfigutation); var forecast = service.GetForecastWeather("Boston","","USA"); forecast = weatherService.GetForecastWeather(this.GeoLocation.Longitude.Value, this.GeoLocation.Latitude.Value);
<div class="text-center"> <h3>Current Weather</h3> <h4>@string.Format("{0:N1}",currentWeather.CurrentCondition.Temperature) <sup>°C</sup> / @string.Format("{0:N1}", tempF) <sup>°F</sup></h4> <img src="@string.Format(" http />/openweathermap.org/img/w/{0}.png", currentWeather.WeatherConditions[0].Icon)" alt="weather conditions"/> <span>@currentWeather.WeatherConditions[0].Status</span> </div>
<div> <h3 class="text-center">Weather Forecast</h3> @foreach (var condition in forecastWeather.WeatherConditions.Take(3)) { var tempF = (condition.CurrentCondition.Temperature * 1.8 + 32); <p><span class="glyphicon glyphicon-calendar"> </span>@condition.TimeOfData.ToShortTimeString() <img src="@string.Format(" http />/openweathermap.org/img/w/{0}.png", condition.WeatherConditions[0].Icon)" alt="weather conditions" /> <span>@condition.WeatherConditions[0].Status</span></p> } </div>
We have published this library as NuGet package. You can install it in your project using NuGet package manager or access it from following location.
WeatherByte packageYou can view this API in action on one of client sites at Topics For Life. If you have any questions, feel free to leave a comment or send us message.
How to host your web site from your home
Internet Explorer does not connect to internet after disabling Verizon wireless access
Internet Explorer file download error with SSL enabled web site
How to plan CCSP Exam preparation
Develop a MongoDB pipeline to transform data into time buckets
Alert and Confirm pop up using BootBox in AngularJS
AngularJS Grouped Bar Chart and Line Chart using D3
How to lock and unlock account in Asp.Net Identity provider
2024 © Byteblocks, ALL Rights Reserved. Privacy Policy | Terms of Use