Posts Tagged ‘Lib’

AS3 Google Weather API lib – Updated

A while ago, I needed to integrate a weather forecast into an actionscript application I was developing.

After some initial searching, I quickly stumbled upon the yahoo weather API.
I dug into this some more and even found an AS3 library for getting the weather forecast! But then, the dissapointment came. When implementing this, you could only get a basic forecast for the current date, with a link to go to the full weather forecast. As demonstrated here.

After some more searching for the right API to suit our needs (a very basic one), I found the google weather API.
The use of the API is as follows:
1) you send a http request like the following : http://www.google.com/ig/api?weather=Paris,France&hl=fr to the API
2) you receive an XML response containing weather forecast data for the next 4 days for a given location
So actually you could say it is more like a feed than an API.

But this seemed to be exactly what I needed to get the job done.
The main problem I ran into was that the API was not officially documented, so I had to do some scraping (monitoring requests and responses with charles) to find out how it worked. And of course putting the pieces together reading several blogs / forums etc.
More unofficial documentation can be found here
Also, I never found a proper actionscript library that enables us to retreive this data. That’s why I decided to stop wasting precious time and just make it myself.

Basically all I did was create a wrapper library that does the request, gets the response and handles errors in an easy to use manner.
This resulted in a fully functional AS3 library for use in flash or flex to retreive weather forecast data from google weather.

Note: When loading the xml and images from an external source, make sure to use a proxy in order to avoid flash player security issues.
In some cases, you also need to put a crossdomain.xml on your server in order to enable your swf to acces external content.

I tried to put comments in the example as much as I could to make everything as clear as possible.
Feel free to use this library in your projects / play around with it.
If you have any remarks, additions, request, questions… just post them here.

This is the basic result, just displaying the data received from the api. Nothing less, nothing more:

UPDATE

Google made changes to the API, causing the standard images not to show anymore.
A minor fix in the GoogleWeatherService.as class solved the issue.

Changed

1
public static const BASE_IMAGE_URL:String = "http://www.google.com/ig";

to

1
public static const BASE_IMAGE_URL:String = "http://www.google.com";

View the new example here
Or download the SWC and source files with the above usage example here: AS3 Google Weather lib (1266)

Tags: , , , ,

43 Comments



SetPageWidth