地圖天氣預報服務:一,獲取天氣預報信息;二,解析天氣預報信息;三,在地圖上加載天氣預報信息;node
Yahoo!提供的天氣預報服務採用流行的RSS輸出結果,接口地址以下:web
http://weather.yahooapis.com/forecstrss?p={location}&u={unit}ajax
location:地點ID或美國郵編 // 地點ID是形如"USCA1116"的字符串api
地區ID能夠到http://weather.yahoo.com/進行查詢跨域
unit:天氣信息的顯示單位。 c(公制)和f(英制)安全
詳細RSS文檔內容:https://developer.yahoo.com/weather/服務器
RSS文檔是一種XML文檔。異步
/*函數
AJAX跨域獲取數據安全行問題,客戶端的Javascript沒法直接加在此RSS文檔,必須在服務器中轉一次。this
同時,文檔中節點含有命名空間,xmlnode.getElementsByTagNameNS() // ie不支持?
*/
GoogleAJAXFeedAPI 主頁:http://code.google.com/apis/ajaxfeeds
GoogleAJAXFeedAPI 支持多種Feed格式
GoogleAJAXFeedAPI 中支持的XML的命名空間,其全局方法爲 google.feeds.getElementByTagNameNS()
google.feeds.getElementByTagNameNS(node,ns,localName)//node:須要查找的根節點 ns:命名空間的 URI localName:本地名稱
返回值爲NodeList類型,爲一組符合條件的XML節點。
Feed()對象 位於 google.feeds 命名空間,三個方法:load()、setNumEntries()和setResultFormat()
(1)由於加載Feed屬於異步調用,因此須要Feed.load()加入回調函數對返回數據進行處理 Feed.load(callbackFunction)
調用時能夠傳遞result參數,result參數的類型由Feed.setResultFormat()決定
(2)Feed.setNumEntries(num)//用於設定返回的Feed中所含的項目數 num爲加載Feed後, result參數中包含的項目數
(3)Feed.setResultFormat(format) 用於設定返回的數據格式
google.feeds.Feed.JSON_FORMAT、google.feeds.Feed.XML_FORMAT、google.feeds.Feed.MIXED_FORMAT ( 混合格式的數據 )
完成處理Feed函數後,google.setOnLoadCallback() 函數設定爲回調函數了。
建立YWeather類(方便重用):
//Url模板
var feedUrlTemplate = "http:\/\/image.weather.yahoo.com\/web\/common\/wxicons\/31\/{code}.gif";
//天氣RSS路徑的模板
var feedUrlTemplate = "http:\/\/weather.yahoo.com\/forecastrss?p={id}&u={unit}";
function YWeather(){};
YWeather.prototype.loadWeather = function(id,unit){
if(!id) return;//若是id爲空
this.id = id;
this.unit = unit?unit:"c";
var url = feedUrlTemplate.replace("{id}",this.id).replace("{unit}",this.unit);//生成真實的RSS的URL
//加載 RSS並設定其回調函數爲this.process,即YWeather.process
this.feed = new google.feeds.Feed(url);
this.feed.setResultFormat(google.feeds.Feed.XML_FORMAT);
this.feed.load(this.process);
}