OpenWeatherMap Weather Web Services For .Net

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.

Get application key

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.

How to get current weather?

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);

How to get weather forecast?

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);

Building MVC shared view for current weather

  <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>

Building MVC view for forecast

<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>

Install NuGet package

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 package

See it live

You 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.

Search

Social

Weather

-1.9 °C / 28.6 °F

weather conditions Clouds

Monthly Posts

Blog Tags