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

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

     
  3. Vivek Shrivastava

    September 3, 2012 at 4:10 am

    Hi i am geeting erro r –>
    The remote server returned an error: (403) Forbidden.

     
  4. Shailesh Singh

    October 10, 2012 at 2:37 am

    This is thow exception (Object reference not set to an instance of an object.) plz correct and send me on

     
    • Shailesh Singh

      October 10, 2012 at 2:49 am

      Plz send me code which will run with asp.net c#

       
  5. Admin

    October 28, 2012 at 12:48 pm

    @Vivek, @Shailesh

    Thanks for pointing this out. Currently this code gets data from google url http://www.google.com/ig/api?weather=new york, Now google stopped this service so there is no data returned as you can try this url directly.
    I will find out any other solution.

     
 
Improve Your Life, Go The myEASY Way™