
APPID(如示例中的 fa2pT26k),妥善保管。latitude, longitude。https://where.yahooapis.com/geocode?location={lat},{lon}&flags=R&appid={APPID}(flags=R 表示反向地理編碼)。woeid、城市/國家名稱。https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid={woeid}&format=json。<!DOCTYPE html>
<html>
<head>
<meta charset="gbk" />
<title>Weather Forecast with jQuery & Yahoo APIs</title>
<link rel="stylesheet" href="assets/css/styles.css" />
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Playball|Open+Sans+Condensed:300,700" />
<!--[if lt IE 9]>
<script src="https://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<header><h1>Weather Forecast</h1></header>
<div id="weather">
<ul id="scroller"><!-- 預報項動態插入 --></ul>
<a href="#" class="arrow previous">Previous</a>
<a href="#" class="arrow next">Next</a>
</div>
<p class="location"></p>
<div id="clouds"></div> <!-- CSS3 動畫雲層 -->
<footer>...</footer>
<script src="https://code.jquery.com/jquery-1.7.2.min.js"></script>
<script src="assets/js/script.js" charset="utf-8"></script>
</body>
</html>
$(function() {
// 配置
var APPID = 'fa2pT26k'; // 替換為你的 APPID
var DEG = 'c'; // 'c' 攝氏 / 'f' 華氏
// Yahoo 天氣代碼 → 圖標映射 (0-47)
var weatherIconMap = [
'storm','storm','storm','lightning','lightning','snow','hail','hail',
'drizzle','drizzle','rain','rain','rain','snow','snow','snow','snow',
'hail','hail','fog','fog','fog','fog','wind','wind','snowflake',
'cloud','cloud_moon','cloud_sun','cloud_moon','cloud_sun','moon','sun',
'moon','sun','hail','sun','lightning','lightning','lightning','rain',
'snowflake','snowflake','snowflake','cloud','rain','snow','lightning'
];
var $weather = $('#weather'),
$scroller = $('#scroller'),
$location = $('p.location');
// 1. 地理位置授權檢查
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(locationSuccess, locationError);
} else {
showError("Your browser does not support Geolocation!");
}
// 2. 位置成功回調 → 調用 PlaceFinder
function locationSuccess(position) {
var lat = position.coords.latitude;
var lon = position.coords.longitude;
var geoAPI = 'https://where.yahooapis.com/geocode?location=' + lat + ',' + lon + '&flags=R&appid=' + APPID;
$.getJSON(geoAPI, function(data) {
if (data.ResultSet.Found > 0) {
var place = data.ResultSet.Results[0];
var woeid = place.woeid;
var city = place.city + ', ' + place.country;
$location.text(city);
fetchWeather(woeid);
} else {
showError("Location not found");
}
});
}
// 3. 獲取天氣數據
function fetchWeather(woeid) {
var yql = 'select * from weather.forecast where woeid=' + woeid;
var weatherAPI = 'https://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent(yql) + '&format=json';
$.getJSON(weatherAPI, function(data) {
var forecasts = data.query.results.channel.item.forecast;
renderForecast(forecasts);
animateClouds(); // 啟動背景雲朵動畫
});
}
// 4. 渲染預報輪播
function renderForecast(forecasts) {
$scroller.empty();
$.each(forecasts, function(i, day) {
var icon = weatherIconMap[parseInt(day.code)];
var high = DEG === 'c' ? celsius(day.high) : day.high;
var low = DEG === 'c' ? celsius(day.low) : day.low;
var html = '<li>' +
'<img src="assets/img/icons/' + icon + '.png" alt="" />' +
'<span class="day">' + day.day + '</span>' +
'<span class="high">' + high + '°</span>' +
'<span class="low">' + low + '°</span>' +
'<span class="condition">' + day.text + '</span>' +
'</li>';
$scroller.append(html);
});
initScroller(); // 綁定 Previous/Next 箭頭輪播邏輯
}
function celsius(f) { return Math.round((f - 32) * 5 / 9); }
function locationError(err) {
var msg = {1: "Permission denied", 2: "Position unavailable", 3: "Timeout"}[err.code] || err.message;
showError("Geolocation error: " + msg);
}
function showError(msg) { $location.html('<span class="error">' + msg + '</span>'); }
});
#clouds 容器內多層雲圖片,配合 @keyframes 實現無限循環漂浮,根據天氣代碼動態切換晴/陰/雨/雪背景班次,極大提升體驗感。
注意:Yahoo Weather/PlaceFinder API 於 2019 年正式關停。現代替代方案:
本教程架構通用,僅需替換 API 端點、參數映射、圖標映射表即可無縫遷移。
從瀏覽器定位到跨域 API 串聯,再到動態渲染與交互動畫,這個小專案濃縮了現代前端開發的核心工作流:權限申請 → 數據編排 → 狀態驅動視圖 → 細節打磨。拿起代碼,換上新 API,你的專屬天氣應用觸手可及。