2007年12月4日星期二

使用 Yahoo Weather RSS feed

使用 Yahoo Weather RSS feed
http://developer.yahoo.com/weather/

1.
FlashLite 2.1 Content 是 352 x 416

2.
準備兩個文字框,一個用於切換城市:
cityName_txt, x=5, y=5

一個用於顯示其他文字資訊:
info_txt, x=5, y=140

3.
frame 1 的程式碼:


/*
http://developer.yahoo.com/weather/
*/
//台灣的城市
var citys:Array = new Array();
citys.push({name:"Ch'i-shan", yahooLocationID:"TWXX0005"});
citys.push({name:"Chang-hua", yahooLocationID:"TWXX0001"});
citys.push({name:"Chi-lung", yahooLocationID:"TWXX0003"});
citys.push({name:"Chia-i", yahooLocationID:"TWXX0002"});
citys.push({name:"Chingmei", yahooLocationID:"TWXX0004"});
citys.push({name:"Chu-tung", yahooLocationID:"TWXX0006"});
citys.push({name:"Feng-yuan", yahooLocationID:"TWXX0007"});
citys.push({name:"Hengch'un", yahooLocationID:"TWXX0008"});
citys.push({name:"Hengchun", yahooLocationID:"TWXX0027"});
citys.push({name:"Hsin-chu", yahooLocationID:"TWXX0009"});
citys.push({name:"Hsin-tien", yahooLocationID:"TWXX0010"});
citys.push({name:"Hua-lien", yahooLocationID:"TWXX0011"});
citys.push({name:"Kangshan", yahooLocationID:"TWXX0012"});
citys.push({name:"Kao-hsiung", yahooLocationID:"TWXX0013"});
citys.push({name:"Miao-li", yahooLocationID:"TWXX0014"});
citys.push({name:"P'ing-tung", yahooLocationID:"TWXX0015"});
citys.push({name:"Su-ao", yahooLocationID:"TWXX0016"});
citys.push({name:"T'ai-chung", yahooLocationID:"TWXX0019"});
citys.push({name:"T'ai-nan", yahooLocationID:"TWXX0020"});
citys.push({name:"T'aipeihsien", yahooLocationID:"TWXX0022"});
citys.push({name:"T'aitung", yahooLocationID:"TWXX0023"});
citys.push({name:"T'ao-yuan", yahooLocationID:"TWXX0025"});
citys.push({name:"Ta-cho-shui", yahooLocationID:"TWXX0017"});
citys.push({name:"Ta-fan-lieh", yahooLocationID:"TWXX0018"});
citys.push({name:"Ta-wu", yahooLocationID:"TWXX0026"});
citys.push({name:"Taidong", yahooLocationID:"TWXX0028"});
citys.push({name:"Taipei", yahooLocationID:"TWXX0021"});
citys.push({name:"Tan-shui", yahooLocationID:"TWXX0024"});
//有的沒的
var xml:XML;
var weather:Object;
var cityIdx:Number = 0;
var image_mc:MovieClip = this.createEmptyMovieClip("image_mc", 2);
image_mc._x = 0;
image_mc._y = 20;
//取得某個城市的RSS資料
function getCityXML(idx:Number) {
var yahooLocationID:String = citys[idx].yahooLocationID;
xml = new XML();
xml.ignoreWhite = true;
xml.onLoad = mx.utils.Delegate.create(this, function (success:Boolean) {
if (success) {
weather = new Object();
var channelXmlNode:XMLNode = this.xml.firstChild.firstChild;
for (var i:Number = 0; i<channelXmlNode.childNodes.length; i++) {
if (channelXmlNode.childNodes[i].nodeName.indexOf("yweather:") != -1) {
weather[channelXmlNode.childNodes[i].nodeName] = new Object();
for (var j in channelXmlNode.childNodes[i].attributes) {
weather[channelXmlNode.childNodes[i].nodeName][j] = channelXmlNode.childNodes[i].attributes[j];
}
} else if (channelXmlNode.childNodes[i].nodeName == "image") {
var imageXmlNode:XMLNode = channelXmlNode.childNodes[i];
weather[imageXmlNode.nodeName] = new Object();
for (var k:Number = 0; k<imageXmlNode.childNodes.length; k++) {
weather[imageXmlNode.nodeName][imageXmlNode.childNodes[k].nodeName] = imageXmlNode.childNodes[k].firstChild.nodeValue;
}
} else if (channelXmlNode.childNodes[i].nodeName == "item") {
var itemXmlNode:XMLNode = channelXmlNode.childNodes[i];
weather[itemXmlNode.nodeName] = new Object();
for (var k:Number = 0; k<itemXmlNode.childNodes.length; k++) {
if (itemXmlNode.childNodes[k].nodeName == "yweather:condition") {
weather[itemXmlNode.nodeName][itemXmlNode.childNodes[k].nodeName] = new Object();
for (var j in itemXmlNode.childNodes[k].attributes) {
weather[itemXmlNode.nodeName][itemXmlNode.childNodes[k].nodeName][j] = itemXmlNode.childNodes[k].attributes[j];
}
} else if (itemXmlNode.childNodes[k].nodeName == "yweather:forecast") {
if (weather[itemXmlNode.nodeName][itemXmlNode.childNodes[k].nodeName] == null) {
weather[itemXmlNode.nodeName][itemXmlNode.childNodes[k].nodeName] = new Array();
}
var obj:Object = new Object();
for (var j in itemXmlNode.childNodes[k].attributes) {
obj[j] = itemXmlNode.childNodes[k].attributes[j];
}
weather[itemXmlNode.nodeName][itemXmlNode.childNodes[k].nodeName].push(obj);
} else {
weather[itemXmlNode.nodeName][itemXmlNode.childNodes[k].nodeName] = itemXmlNode.childNodes[k].firstChild.nodeValue;
}
}
} else {
weather[channelXmlNode.childNodes[i].nodeName] = channelXmlNode.childNodes[i].firstChild.nodeValue;
}
}
endLoad();
} else {
trace("failed");
}
});
xml.load("http://weather.yahooapis.com/forecastrss?p="+yahooLocationID+"&u=c");
startLoad();
}
//開始載入資料
function startLoad() {
//image
image_mc.unloadMovie();
//info
info_txt.text = "";
}
//載入RSS完成
function endLoad() {
//image
var imgPath:String = "http://us.i1.yimg.com/us.yimg.com/i/us/nws/weather/gr/"+weather.item["yweather:condition"].code+"d.png";
image_mc.loadMovie(imgPath);
//info
var str:String = "";
str += ("geo:lat="+weather.item["geo:lat"]+", geo:long="+weather.item["geo:long"]+"\n");
str += ("\n");
str += ("text="+weather.item["yweather:condition"].text+", code="+weather.item["yweather:condition"].code+"\n");
str += ("temp="+weather.item["yweather:condition"].temp+", date="+weather.item["yweather:condition"].date+"\n");
for (var i:Number = 0; i<weather.item["yweather:forecast"].length; i++) {
str += ("\n");
str += ("day="+weather.item["yweather:forecast"][i].day+", date="+weather.item["yweather:forecast"][i].date+"\n");
str += ("low="+weather.item["yweather:forecast"][i].low+", high="+weather.item["yweather:forecast"][i].high+"\n");
str += ("text="+weather.item["yweather:forecast"][i].text+", code="+weather.item["yweather:forecast"][i].code+"\n");
}
info_txt.text = str;
}
//更新城市文字框
function updateCityText() {
cityName_txt.text = citys[cityIdx].name;
getCityXML(cityIdx);
}
//上下鍵切換城市
var keyListener:Object = {onKeyUp:mx.utils.Delegate.create(this, function () {
switch (Key.getCode()) {
case Key.UP :
cityIdx = cityIdx>0 ? cityIdx-1 : 0;
updateCityText();
break;
case Key.DOWN :
cityIdx = cityIdx<citys.length-1 ? cityIdx+1 : citys.length-1;
updateCityText();
break;
}
})};
Key.addListener(keyListener);
//第一次更新城市文字框
updateCityText();




4.
執行結果:



Yahoo Weather 的網頁:http://weather.yahoo.com/forecast/TWXX0005_c.html



Yahoo Weather 的網頁:http://weather.yahoo.com/forecast/TWXX0021_c.html

============================================

[2007-12-04-22-22]

在手機能上網的環境,用我的 Nokia E70 實際測試:



放成全螢幕:

0 意見:

關於我






* ben {dot} chang {at} ben {dot} idv {dot} tw
* FriendFeed

贊助我1元美金:

Plurk

標籤雲