RSS
 

Google Weather API

19 Feb
Google Weather Provides weather information from the world. They accepts a wide range of parameters like [city],[country] e.g. London,England – or postcodes, ISO country codes (UK,DE etc.), Lat/long.

To integrate it with asp.net just use below function and it will return weather information for current and next 4 days with images according to weather.

public static void GoogleWeather(string location)
    {
        HttpWebRequest GoogleRequest;
        HttpWebResponse GoogleResponse = null;
        XmlDocument GoogleXMLdoc = null;
        try
        {
            GoogleRequest = (HttpWebRequest)WebRequest.Create("http://www.google.com/ig/api?weather=" + string.Format(location));
            GoogleResponse = (HttpWebResponse)GoogleRequest.GetResponse();
            GoogleXMLdoc = new XmlDocument();
            GoogleXMLdoc.Load(GoogleResponse.GetResponseStream());

            XmlNode root = GoogleXMLdoc.DocumentElement;

            XmlNodeList nodeList1 = root.SelectNodes("weather/forecast_information");
            HttpContext.Current.Response.Write("City : " + nodeList1.Item(0).SelectSingleNode("city").Attributes["data"].InnerText + "");

            XmlNodeList nodeList = root.SelectNodes("weather/current_conditions");
            HttpContext.Current.Response.Write("
");
            HttpContext.Current.Response.Write("
" + nodeList.Item(0).SelectSingleNode("temp_c").Attributes["data"].InnerText + " °C | " + nodeList.Item(0).SelectSingleNode("temp_f").Attributes["data"].InnerText + " °F"); HttpContext.Current.Response.Write("Current: " + nodeList.Item(0).SelectSingleNode("condition").Attributes["data"].InnerText + ""); HttpContext.Current.Response.Write("" + nodeList.Item(0).SelectSingleNode("wind_condition").Attributes["data"].InnerText + ""); HttpContext.Current.Response.Write(nodeList.Item(0).SelectSingleNode("humidity").Attributes["data"].InnerText); nodeList = root.SelectNodes("descendant::weather/forecast_conditions"); foreach (XmlNode nod in nodeList) { HttpContext.Current.Response.Write(" " + nod.SelectSingleNode("day_of_week").Attributes["data"].InnerText + ""); HttpContext.Current.Response.Write("" + nod.SelectSingleNode("condition").Attributes["data"].InnerText + ""); HttpContext.Current.Response.Write(nod.SelectSingleNode("low").Attributes["data"].InnerText + "°F | "); HttpContext.Current.Response.Write(nod.SelectSingleNode("high").Attributes["data"].InnerText + "°F"); } HttpContext.Current.Response.Write("
"); } catch (System.Exception ex) { HttpContext.Current.Response.Write(ex.Message); } finally { GoogleResponse.Close(); } }

Download : Download

 
2 Comments

Posted in APIs, Weather

 

Tags: ,

Leave a Reply

 

 
 
  1. Satheesh

    February 24, 2010 at 11:49 am

    Hi,
    The code is not working if we give the ISO country code. I tried UK and it returned error as “Object reference not set to an instance…”.

    Regards,
    Satheesh

     
  2. admin

    February 24, 2010 at 1:56 pm

    It will not work if we give country code or even full country name alone as weather will different in different cities in a country. You can use country code in conjunction with [city, country code] or [postcode,countrycode]

     
 
Improve Your Life, Go The myEASY Way™