I thought this post would be useful as a follow on from my last post “Using Google Docs As A Webservice”, where I showed how you can use the ‘publish as ATOM feed’ feature of Google Spreadsheet to read dynamic data from your mobile app using PhoneGap and XUI.
In this installment, I’m adding the ability to present the weather in my mobile app (still using PhoneGap and XUI), building on what we used in the previous post. Let’s have a look:
As the title suggests, Google have an unofficial API for retrieving weather information, that is simply a GET request to an iGoogle URL. The one I’m using for CAPE TOWN is:
http://www.google.co.za/ig/api?weather=Cape+Town&hl=en_US
As you might , you simply specify your city as the ‘weather’ parameter, eg:
[New York City] http://www.google.co.za/ig/api?weather=New+York+City&hl=en_US
[Tokyo, Japan] http://www.google.co.za/ig/api?weather=Tokyo,Japan&hl=en_US
If you examine the response XML, you’ll notice some things:
– You get the ‘current conditions’, Today’s conditions and the next 3 day forecast.
– The wind speed is returned in MPH.
– The forecast low and high temps are in Fahrenheit
Let’s see some code:
igweather.js
Here, I’ve created 2 functions: loadCurrentWeather() and loadForecastWeather() -> They sound be self explanatory:
http://paste.org/pastebin/api/35517
weather.html
Here is a snippet of the page body to display the weather data. The Javascript and HTML files make “heavy” use of element ids (to separate presentation from data model):
Notice that I load the functions when the body loads.
http://paste.org/pastebin/api/35518
and this is what my implementation looks like:
I hope you find this post helpful. If you find any errors or and have tips or crits, I’d love to hear from you!
Thanks for reading! Happy hacking.
Bernhard
June 28, 2011 at 14:55
Three cheers for Cape Town! The place of my youth! 🙂
mitchwongho
June 29, 2011 at 10:21
*High 5* =)
petere
July 11, 2011 at 03:34
Can you this with jquery mobile.?
mitchwongho
July 11, 2011 at 09:34
Hi,
Thanks for visiting.
Not directly by JQuery mobile, but by JQuery (since JQuery mobile is a dependent on JQuery)
BlackKnight
July 14, 2011 at 10:54
I don’t understand this symbols “�” in weather.html
and i tested but the code not working
mitchwongho
July 14, 2011 at 11:03
Hi,
Thanks for visiting.
The “�” are actually ‘ ‘ codes. I think it was converted by paste.org.
I hope it helps
BlackKnight
July 14, 2011 at 11:33
Can u send me the final files?
linhmail288@yahoo.com.vn
many thanks.
Vicky Singh
November 1, 2011 at 22:23
can you email the code files to vicky.singh@yahoo.com
Monica
March 8, 2012 at 15:04
please can u send me the file ‘igweather.js’ and ‘weather.html’ at monica.sahoo13@gmail.com.
Its urgent
Hawaii Airport
August 3, 2012 at 23:00
Pretty! This was an incredibly wonderful post.
Many thanks for providing this info.
Kos Kesh
August 7, 2012 at 20:14
why bother posting this if it doesnt work the way it is portrayed?
Alex Lima
August 14, 2012 at 00:41
Great 5 stars!!!
Please send final files to pcsclima@hotmail.com
Denn
August 31, 2012 at 13:16
Google Weather API was down
You can use another free weather API
http://openweathermap.org
See more about JSON and WMS api http://openweathermap.org/wiki/Main_Page