先到 https://weather.yahoo.com/ 查天氣區域代碼,例如輸入 Kaohsiung 查詢,
就會顯示 Kaohsiung City的天氣,此時網址為:
https://weather.yahoo.com/taiwan/kaohsiung-city/kaohsiung-city-2306180/
其中的2306180即為高雄市代碼。
再利用Yahoo Weather API
https://developer.yahoo.com/weather/
輸入YQL:
select item.condition from weather.forecast where woeid = 2306180 and u="c"
u="c"代表用攝氏表示,預設為華氏,在Endpoint就會產生所需要的JSON(或XML)資料回傳網址。
回傳的JSON資料,key=value可參考 https://developer.yahoo.com/weather/documentation.html
之後就可以在網頁上顯示天氣資料了,譬如要顯示天氣狀況和溫度:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | $.getJSON( 'https://query.yahooapis.com/v1/public/yql?q=select%20item.condition%20from%20weather.forecast%20where%20woeid%20%3D%202306180%20and%20u%3D%22c%22&format=json&callback=?' , function (json) { var w_code = weather_con[json.query.results.channel.item.condition.code.toString()]; var w_temp = json.query.results.channel.item.condition.temp + "℃" ; console.log(w_code); console.log(w_temp); }); //這個我翻得可能不是很標準... weather_con = { "0" : "龍捲風" , "1" : "熱帶風暴" , "2" : "颶風" , "3" : "強雷陣雨" , "4" : "雷陣雨" , "5" : "混合雨雪" , "6" : "混合雨雪" , "7" : "混合雨雪" , "8" : "冰凍小雨" , "9" : "細雨" , "10" : "凍雨" , "11" : "陣雨" , "12" : "陣雨" , "13" : "飄雪" , "14" : "陣雪" , "15" : "吹雪" , "16" : "下雪" , "17" : "冰雹" , "18" : "雨雪" , "19" : "多塵" , "20" : "多霧" , "21" : "陰霾" , "22" : "多煙" , "23" : "狂風大作" , "24" : "有風" , "25" : "冷" , "26" : "多雲" , "27" : "晴間多雲(夜)" , "28" : "晴間多雲(日)" , "29" : "晴間多雲(夜)" , "30" : "晴間多雲(日)" , "31" : "清晰的(夜)" , "32" : "晴朗" , "33" : "晴朗(夜)" , "34" : "晴朗(日)" , "35" : "雨和冰雹" , "36" : "炎熱" , "37" : "雷陣雨" , "38" : "零星雷陣雨" , "39" : "零星雷陣雨" , "40" : "零星雷陣雨" , "41" : "大雪" , "42" : "零星陣雪" , "43" : "大雪" , "44" : "多雲" , "45" : "雷陣雨" , "46" : "陣雪" , "47" : "雷陣雨" , "3200" : "資料錯誤" }; |
沒有留言:
張貼留言