yahoo AS 3.0 weather API with flash

Here’s a pretty simple AS 3.0 example of using the yahoo weather API in flash. Nothing too fancy going on here. I plan to use it for something just a little more interesting than this, but I wanted to share this simple version because there aren’t a lot of non-flex samples out there.

You can see the sample here.

Start out by creating an instance of the WeatherService class:

//establish the weather service class
var ws:WeatherService = new WeatherService;
var w:Weather = new Weather;

Then, use the getWeather method to receive the weather information you’re looking for.

//grab the weather restults when the button is pressed
function getMyWeather(event:MouseEvent):void {
//use the zip code entered to get the weather
loc = zip_txt.text;
ws.getWeather(loc, "ENGLISH_UNITS");

getWeather takes just two parameters: your location and the type of units you’d like returned. My sample file is using US zip codes, but there are other location options available.
The full list of APIs available and documentation are here.

And here’s the FLA source for the flash sample.

  • mike bodily

    Sorry, but I get the following errors when I try and compile your source file:

    1046: Type was not found or was not a compile-time constant: WeatherResultEvent.

  • val

    hi mike,

    you’ll get that error if you don’t have the yahoo API classes downloaded and referenced as well. I didn’t include them with my source, but you can grab them off the yahoo site here:

    copy the com folder into the same directory as sample file and you’ll be good to go. If you prefer the API files somewhere else, just edit the import statements at the top of the example file.