Sunday, February 26, 2012

Weather Geocoder with Google Maps and Google Weather API

Google Weather GeocoderI got tired of searching locations on weather reporting websites to get instant updates, so I thought of building an instant weather reporting application which would give me information for any location I click on the map. Google Maps API provides an inbuilt reverse-geocoding service which can geocode an IP Address to a place on the map. To get weather information, I’ve used the so called secret Weather API from Google which they use on the iGoogle page widget. Google has not officially published this API but we would love if they do so. The good thing about Google’s weather API is that it is easy to use and includes the weather icons, but there is no way as of now to get the data in other formats apart from XML:
http://www.google.com/ig/api?weather=Chicago.

Demo on my Labs page: http://lab.abhinayrathore.com/geocode_weather/

You can play around with the app on my labs page and view the source on the page to get the JavaScript code to build this tool on your own website. To convert the XML data from Google Weather API to JSONP, I’ve written a small PHP script you can download from the link below.

Click here to get the PHP Script for converting Google Weather API’s XML data to JSON/JSONP format.

You can style the Weather Information window according to your needs or you can use the one I’ve created: http://lab.abhinayrathore.com/geocode_weather/weather.css. You can freely modify the JavaScript code to suit your needs. If you find any bugs or see improvement opportunities, please do drop a comment below and I’ll look into it asap :)

8 comments:

  1. Thanks for sharing your info. I really appreciate your efforts and I will be waiting for your further write ups thanks once again.
    html5 video player

    ReplyDelete
  2. hi, nicely done there great job! and thanks for sharing
    but i have a question...
    is there a way to overcome the PHP Script for converting Google Weather API’s XML data to JSON/JSONP format and converting the original xml file google creates at "http://www.google.com/ig/api?weather=Chicago" to plain html using jquery?

    ReplyDelete
  3. Anonymous,
    Instead of converting the XML to JSONP (I designed it for cross-domain access), you can write a simple proxy script in any language to fetch the Google XML from your JavaScript code. Then you can parse the XML file in jQuery (http://api.jquery.com/jQuery.parseXML/) and get the weather data in formatWeatherData() method.

    ReplyDelete
  4. i've tried that exactly,but i get (in google chrome's inspect element) this error...
    "XMLHttpRequest cannot load file:///C:/Users/andry/Desktop/2.xml. Origin null is not allowed by Access-Control-Allow-Origin."

    and i cant rly think what i did wrong there, thank you again in advance

    ReplyDelete
  5. If you have the XML file locally, then try placing it in the same folder as your script and simply use "2.xml" (without any path) in the ajax call. The best option is to use a proxy script to get the xml data.

    ReplyDelete
  6. Hi,
    The weather google api cannot be used. They treat thee request to get the xml file as an automated query, and just outputs this message:
    We’re sorry… … but your computer or network may be sending automated queries. To protect our users, we can’t process your request right now.

    ReplyDelete
  7. i am getting this error when using your demo page. I am using firefox 13.0.1

    'Error occured during fetching weather data."

    Glenn

    ReplyDelete
  8. Hi. I converted your app to a python/google app engine application. I used weather underground api and it is working fine.

    ReplyDelete

Thanks a lot for your valuable comments :)