RSS

Creating a Weather Widget with XML and AS3

Wed, Jul 16, 2008

Flash, Webservices, XML

Creating a Weather Widget with XML and AS3

In this tutorial I’m presenting you a little weather widget, that could come handy for travel websites, or our personal page. If you make some changes on the code, you could show a different interface on your website, according to the weather on your city. Pretty cool stuff. We gonna connect weather.com XML data file with our flash widget. Weather.com offers us a free service to do that.

Requirements

Adobe Flash CS3

Try / Buy

Source Files

Download

Sign up to weather.com

The first thing we gonna do is login in our weather.com account:

https://registration.weather.com/ursa/profile

Or if we already don’t have an account we could register for free in here:

https://registration.weather.com/ursa/profile/new?

Registering for weather.com XML Data Feed

Once we are in our account, we gonna have a page like this one, where on the left we gonna have all the services we are subscribed
and on the right, all the available services, the XML data feed is not there, it’s a little bit tricky.

So, we gonna signup to the XML data feed on this page

https://registration.weather.com/ursa/xmloap/

Where we gonna tell to weather.com in which way we gonna use the data. If it’s for personal purpose, there’s no problem, but
if it’s commercial purpose, you have to contact them, but everything is better explained on the mail you are going to receive.

So if our registration it’s oki, we gonna receive an email like this one (check on the spam inbox too, just in case).

The email explains us about the service, and there’s three important things to see in here:

  • Where to download the weather.com SDK (http://download.weather.com/web/xml/sdk.zip)
  • The weather.com XML Partner ID & License Key
  • How to make a properly formatted XML request for the service

Contents of the wheater.com SDK

On this package you gonna find a PDF, with the license agreement and some useful information about how to make a request for the services,
and two folder, one with the weather.com logos, and the other with the weather icons we gonna use on this tutorial as reference.
There’s 3 type of icon sizes, for this tutorial we gonna use the 93×93 icons.

The icons are not too fancy, so we could download a free cool set of weather icons from here:

http://liquidweather.net/icons.php#iconsets

This iconsets and backgrounds are from the Liquid Weather++ appi.

Setting up our api key

We could put all our data inside flash, like the code of our city, our partner id, and so on, but it’s gonna be a pain in the ass every time we want to change it.
So, it’s much better if we load this type of data externally form an XML file. The XML structure should looks like this:

	ARBA0009
XXXXXXXXX
	XXXXXXXXXX
	m

Loading our data in Flash

Now our hands on AS.

We gonna do all the programming in just 2 frames, pretty easy.

On the first frame, we load all the data to use it when we call the weather.com xml service.

On the second frame, called “weather”, we load the weather.com xml and display the data.

So, in our first frame:

  • We define the variables we gonna use
  • We call the xml that holds our data
  • Then we pass the data from the xml to our variables
  • And if everything it’s oki, we could go to the weather frame

stop();

//=================
//ini variables
//=================

//we define our variables to be loaded

var city:String;
var par_id:String;
var key:String;
var units:String;

//call the xml with our data

var data_xml_url:String = "data.xml";
var user_data:XML = new XML();
var data_url:URLRequest = new URLRequest(data_xml_url);
var dataLoader:URLLoader = new URLLoader(data_url);

dataLoader.addEventListener(Event.COMPLETE, dataLoaded);

function dataLoaded(e:Event):void
{
	user_data = XML(dataLoader.data);

	//pass the data from the XML to our variables

	city = user_data.city.toString();
	par_id  = user_data.parid.toString();
	key  = user_data.key.toString();
	units  = user_data.units.toString();

	//if everything it's oki, we could go to the weather frame

	gotoAndStop("weather");
}

The weather icons

To display the different type of weather, we need a movieClip that holds all the possible weathers, there’s not too much, just 47.
So, we create a new movieclip and call it “icons_mc”, and inside the icons_mc we import all the icons from the Weather.com SDK icons folder,
or from the cool icons from liquidweather. There’s a lil bit differences between the Weather.com SDK icons and the liquidweather icons.
On the weather.com SDK, the icons 25 and 44 are N/A, if you gonna use the liquidweather icons, just replace the 25 and 44 icons with the N/A png.

Don’t forget to put a stop(); in the first frame of the icons_mc, so the MC start stopped.

Loading the weather.com xml service

Now we could load the data from weather.com

We allow our swf to make calls from the domain “xoap.weather.com”, we make the icons_mc MovieClip invisible, because we don’t know which icon use yet, and we compose the weather xml url with the variables from the frame 1.

stop();
//=================
//allow domains
//=================
Security.allowDomain("*", "xoap.weather.com");
//=================
//ini settings
//=================
icons_mc.visible = false;
//=================
//XML
//=================
var weather_xml_url:String = "http://xoap.weather.com/weather/local/"+city+"?cc=*&link=xoap&par="+par_id+"&key="+key+"&unit="+units;

The xml from weather.com have this structure with some interesting data. As you can see there’s a lot of useful data to load, but we gonna load just the necessary by now.

Later you could play with it and make an advanced one.

    en_US
MEDIUM
C km km/h mb mm Buenos Aires, Argentina 1:39 PM -34.61 -58.37 8:01 AM 5:52 PM -3 6/27/08 1:00 PM Local Time Buenos Aires Air Park, Argentina 13 13 Fog 20 1021.0 falling 11 N/A 360 N 82 2.5 2 Low 10 23 Waning Crescent

Displaying the data

To display the data, we have three different sprites:

  • The icons MovieClip
  • A dynamic text field to load the outside temp called “temp_txt”
  • A dynamic text filed in the bottom to make a lil speech about how feels to be outside called “info_txt”

Our main interest is on the XML file on the icon number, so we could show the right icon.

We load the xml using our well formatted “weather_xml_url”, and traverse the xml three to find what we are looking for.

In this step we:

  • we load the temp on the temp_txt text field
  • make the icons visible, load the icon number and tell the icons:mc to stopn on thet icon frame
  • and finally we set the complementary text
var weather:XML = new XML();
var weather_url:URLRequest = new URLRequest(weather_xml_url);
var weatherLoader:URLLoader = new URLLoader(weather_url);

weatherLoader.addEventListener(Event.COMPLETE, weatherLoaded);

function weatherLoaded(e:Event):void
{
	weather = XML(weatherLoader.data);

	//we load the temp on the temp_txt text field
	temp_txt.text = weather.cc.tmp;

	//we make the icons visible, load the icon number and tell the icons:mc to stopn on thet icon frame
	icons_mc.visible = true;

	var weather_icon:int = Number(weather.cc.icon.toString())+1;
	icons_mc.gotoAndStop(weather_icon);

	//and finally we set the complementary text
	var ud:String = weather.head.ud;
	var us:String = weather.head.us;

	var city:String = weather.loc.dnam;
	var time:String = weather.loc.tm;
	var temp:String = weather.cc.tmp;
	var flik:String = weather.cc.flik;
	var term:String;

	if (temp == flik)
	{
		term = "and it feels like "+flik+" degrees,";
	} else {
		term = "but it feels like "+flik+" degrees,";
	}

	var wind_v:String = weather.cc.wind.s;
	var wind_gust:String = weather.cc.wind.gust;
	var wind_d:String = weather.cc.wind.d;
	var wind_t:String = weather.cc.wind.t;

	var hmid:String = weather.cc.hmid;
	var vis:String = weather.cc.vis;

	info_txt.text = "It's "+time+" here in "+city+". It's "+temp+" degrees out there "+term+" the wind blows from the "+wind_t+" at "+wind_v+" "+us+", the humidity is "+hmid+"% and the visibility is "+vis+" "+ud+".";
}

Now we could export our movie and if it’s cold outside, it’s good to be inside.

Final thoughts

We gonna need to know the international code of our city, so we could go to

http://www.weather.com/common/welcomepage/world.html

Search for our city, and find the code on our address bar. I know it’s a lil bit tricky but it works fine.

Hope you enjoyed this lil tutorial as much as I enjoyed writing for you.

Share/Save/Bookmark

, , , , , , ,

This post was written by:

Fausto Carrera - who has written 2 posts on The Tech Labs.

Fausto Carrera is a industrial designer natural from Ecuador but living in Argentina. He have 8 years of experience developing websites with html and Flash, since php3 and Flash 5. His strengths are developing applications with Flash mixing it with MySQL and PHP, because Flash have a huge potential to deliver better multimedia experience. He have personal blog at www.faustocarrera.com.ar

13 Comments For This Post

  1. jake Says:

    Cool, nice tutorial
    Keep them coming

  2. John Says:

    great tutorial! i have some questions though. are we only limited to the some of the xml data? i’d like to add the day and date information to the widget, so i’m trying to pull out the attributes “t” and “dt” in the day “0″ child element under dayf into a text box, but it’s not showing up. i’m probably doing it wrong as i’m very new to AS3.0 & XML. is there any way you could help me? thanks!

  3. Abel Says:

    I get an ERROR:

    Error #2044: Unhandled securityError:. text=Error #2048: Security sandbox violation: http://www.worldkit.com/weather/weather.swf cannot load data from http://xoap.weather.com/weather/local/35216?cc=*&link=xoap&prod=xoap&par=1072119334&key=4d4b9329018d4592&unit=s.
    at weather_fla::MainTimeline/frame2()

  4. Fusionice Says:

    Anyway to change the language or locale?

  5. Fausto Carrera Says:

    @Fusionice: I’m not sure, but check the PDF on the SDK zip file, sure you could finde the call to change the lang and locale.

    @Abel: I’m at linux right now, so let me try to figure out why it’s the sandbox violation.

  6. canavrio roco Says:

    John, ¿have you tried to replace the line:
    var time:String = weather.loc.tm;

    to this one:
    var time:String = weather.cc.lsup;
    ?

  7. Jon Hnefill Says:

    I get the exact same sandbox violation error as Abel.

    Haven’t found a fix yet despite creating a crossdomain.xml file and placing it on the server.

    Have you manage to sort anything out ?

    /Johnny

  8. VIctor Says:

    i have the same problem it will not connect to the weather page, i change the security permitions in the adobe page and it works but only wen you test it in you computer but when is in the server it dosent work, PLEASE HELP i need to find why.

    thanks

  9. Sudman Says:

    Is anyone else having issues when publishing to a server the flash movie no longer gets the weather feed but on my local machine it works just find. Is there a fix for this? Thanks.

  10. Tim Tonsel Says:

    John, I don’t know if you figured the multiple day forecast out but here is an example I made shows city, cc, days 1,2 and 3 temperature, Icon and day of the week.

    Hope it helps..

    Tim

    function weatherLoaded(e:Event):void
    {
    weather = XML(weatherLoader.data);
    temp_txt.text = weather.cc.tmp;
    day2_txt.text = weather.dayf.day[1].hi;
    day3_txt.text = weather.dayf.day[2].hi;
    day4_txt.text = weather.dayf.day[3].hi;
    text2_txt.text = weather.dayf.day[1].attribute(”t”);
    text3_txt.text = weather.dayf.day[2].attribute(”t”);
    text4_txt.text = weather.dayf.day[3].attribute(”t”);

    icons_mc.visible = true;

    var weather_icon:int = Number(weather.cc.icon.toString())+1;
    var weather2_icon:int = Number(weather.dayf.day[1].part[0].icon.toString())+1;
    var weather3_icon:int = Number(weather.dayf.day[2].part[0].icon.toString())+1;
    var weather4_icon:int = Number(weather.dayf.day[3].part[0].icon.toString())+1;
    icons_mc.gotoAndStop(weather_icon);
    day2_mc.gotoAndStop(weather2_icon);
    day3_mc.gotoAndStop(weather3_icon);
    day4_mc.gotoAndStop(weather4_icon);

    var city:String = weather.loc.dnam;
    var time:String = weather.loc.tm;
    var temp:String = weather.cc.tmp;
    var day2:String = weather.dayf.day[1].hi;
    var day3:String = weather.dayf.day[2].hi;
    var day4:String = weather.dayf.day[3].hi;
    var text2:String = weather.dayf.day[1].attribute(”t”);
    var text3:String = weather.dayf.day[2].attribute(”t”);
    var text4:String = weather.dayf.day[3].attribute(”t”);

    info_txt.text = city;
    day2_txt.text = day2;
    day3_txt.text = day3;
    day4_txt.text = day4;
    text2_txt.text = text2;
    text3_txt.text = text3;
    text4_txt.text = text4;
    }

  11. Anu Says:

    I’m also having the same problem Abel, Jon Hnefill…… The thing works fine on desktop when the security settings are chnaged on the adobe page… but not on websitee!!! PLzzzz help!!!

  12. Ben Faubion Says:

    Great Tutorial.. but i cant get this working either. I’m using this service to get the location code.. for example http://xoap.weather.com/search/search?where=Dallas

    works great in flash, but i just can get past the sandbox issue.. tried all the recommendations and I’m stuck at this point.. tried it with my key and partner id as well.

  13. pheugoo Says:

    preview in flash does work
    but once it is published i loose all the fancy weather stuff!

5 Trackbacks For This Post

  1. Link Post Sunday 07/20 | The Helion Code Says:

    [...] The tech labs created a great tutorial on how to make a weather widget with flash and AS3 [...]

  2. Templates247.com: Flash Tutorials » Blog Archive » Creating a Weather Widget with XML and AS3 Says:

    [...] Read the Tutorial: Creating a Weather Widget with XML and AS3 [...]

  3. Tecinfor » Blog Archive » Incorporar a informação meteorológica em tempo real com AS3 Says:

    [...] weather widget [...]

  4. Link Post Sunday 07/20 | Mr Sun Studios Says:

    [...] The tech labs created a great tutorial on how to make a weather widget with flash and AS3 [...]

  5. 2idea 在变化中继续向前 » Creating a Weather Widget with XML and AS3 Says:

    [...] http://www.thetechlabs.com/xml/creating-a-weather-widget-with-xml-and-as3/ [...]

Leave a Reply