如何使用最新 Google Weather API 打造簡易級天氣應用程式

Picture of Julia Wu

Julia Wu

Marketing, Master Concept
Google Weather API

天氣資訊是影響我們日常生活與商業運營的關鍵因素。無論是規劃通勤、安排戶外活動,或是進行物流調度、風險評估,準確且即時的天氣數據都扮演著至關重要的角色。您是否思考過如何將專業級的天氣資訊整合到您的應用程式、網站或商業分析流程中?2025 Google Cloud Next 最新推出的 Google Weather API 正是為此而生,它為開發者和企業提供了一個強大且易於使用的解決方案。本篇文章將深入介紹此 API 的核心功能、運作原理,並透過實例(以香港為例)展示如何利用它來建構一個能顯示即時天氣狀況與未來一週預報的應用程式。

Afilab - weather API 使用示意圖
Googel Weather API 打造簡易天氣 App 示意圖
Source: 思想科技

什麼是 Google Weather API?

Google Weather API 是 Google Maps Platform 最新發布的「環境與地理空間分析工具 ( geospatial analytics tools)」系列中的核心功能之一。該系列旨在透過提供更精確、更深入的真實世界洞察,支援更智慧的商業決策與應用開發。此系列除 Weather API 外,還包含 Places Insights API(地點商業洞察)、Photorealistic 3D Tiles(高擬真 3D 地圖瓦片)以及 Air Quality API(空氣品質指數)等重要工具。

📚 延伸閱讀:Google Cloud Next 2025 重磅發表:Google Maps Platform 四大新功能深度解析與產業應用前瞻

Weather API 的核心能力在於,讓使用者能夠擷取全球任何指定地點的 即時超在地化 (hyperlocal) 的當前天氣狀況與預測數據。所謂「超在地化」,意指其數據精度能達到非常精細的地理範圍,遠超傳統城市級別的預報。API 提供極為全面的天氣參數,包括溫度、降水機率與強度、風速風向、紫外線指數、相對濕度、氣壓等多維度資訊。這使得開發者能輕易地將 Weather API 與交通路況 (如 Google Routes API)、地理空間數據 (如 Google Geocoding API) 或其他環境數據源結合,創建出具備高度「天氣感知」能力的創新應用。

Google Weather API 如何運作?

Google Weather API 的數據來自多元的氣象觀測來源。其獨特之處在於,Google 運用了其專有的 整合式預報系統 (Integrated Forecasting System, IFS) 來處理與分析這些原始數據。此系統巧妙地融合了 人工智慧 (AI) 機器學習技術與物理學技術,能夠生成全球範圍內任意地點的高解析度即時天氣、未來預報(包含短期與中期)以及歷史天氣回溯數據。

值得注意的是,Google 已將此先進的天氣模型應用於其搜尋引擎的天氣結果展示超過一年,並獲得了廣泛驗證。而 Google Weather API 的發布,則是 Google 首次 將此強大的自研天氣模型能力,以 API 的形式正式開放給開發者社群與企業用戶使用。

此 API 主要透過以下三個核心端點 (Endpoint) 提供服務:

  • /currentConditions:提供指定地點當下的詳細天氣狀況。
  • /forecast:提供未來長達 10 天每日天氣預報,以及精細至每小時的天氣預測。
  • /history:提供指定地點過去 24 小時內(從請求時間點回溯)的每小時歷史天氣數據。

實務操作 (一):取得「即時天氣狀況」(/currentConditions)

呼叫 /currentConditions 端點的請求相當直接,主要需提供目標地點的地理座標(緯度與經度):

GET https://weather.googleapis.com/v1/currentConditions:lookup?key={YOUR_API_KEY}&location.latitude={LATITUDE}&location.longitude={LONGITUDE}&unitsSystem={UNITS}

  • {YOUR_API_KEY} (必要參數): 您在 Google Cloud Platform 上設定並已授權使用 Weather API 的 API 金鑰。這是驗證您請求權限的憑證。
  • {LATITUDE} (必要參數): 查詢地點的緯度座標 (例如:香港 22.302711)。
  • {LONGITUDE} (必要參數): 查詢地點的經度座標 (例如:香港 114.177216)。
  • {UNITS} (非必要參數): 指定回傳數據的單位系統。可選 METRIC (公制,預設值,如攝氏溫度、公里/小時風速) 或 IMPERIAL (英制,如華氏溫度、英里/小時風速)。

補充說明: API 金鑰的取得流程通常涉及在 Google Cloud Console 建立專案、啟用 Google Maps Platform 及 Weather API 服務,並產生專用的 API 金鑰。建議參考官方文件以獲取詳細步驟,並妥善管理您的金鑰安全。

以下是針對香港 (22.302711, 114.177216) 查詢 /currentConditions 的部分 JSON 範例:

{
    "currentTime": "2025-04-18T09:03:56.202537583Z",
    "timeZone": {
        "id": "Asia/Hong_Kong"
    },
    "isDaytime": true,
    "weatherCondition": {
        "iconBaseUri": "https://maps.gstatic.com/weather/v1/cloudy",
        "description": {
            "text": "Cloudy", // 天氣描述文字 [cite: 16]
            "languageCode": "en" [cite: 16]
        },
        "type": "CLOUDY"
    },
    "temperature": { // 溫度 [cite: 16]
        "degrees": 24.9,
        "unit": "CELSIUS"
    },
    "feelsLikeTemperature": { // 體感溫度 [cite: 17]
        "degrees": 27.6,
        "unit": "CELSIUS"
    },
    "dewPoint": { // 露點 [cite: 17]
        "degrees": 23.1,
        "unit": "CELSIUS" [cite: 17]
    },
    "heatIndex": { // 酷熱指數 [cite: 17]
        "degrees": 27.6,
        "unit": "CELSIUS"
    },
    "windChill": { // 風寒指數 [cite: 17]
        "degrees": 24.9,
        "unit": "CELSIUS"
    },
    "relativeHumidity": 90, // 相對濕度 [cite: 18]
    "uvIndex": 0, // 紫外線指數 [cite: 18]
    "precipitation": { // 降水 [cite: 18]
        "probability": {
            "percent": 20,
            "type": "RAIN" [cite: 18]
        },
        "snowQpf": {
            "quantity": 0,
            "unit": "MILLIMETERS"
        },
        "qpf": {
            "quantity": 0,
            "unit": "MILLIMETERS" [cite: 19]
        }
    },
    "thunderstormProbability": 40, // 雷暴機率 [cite: 19]
    "airPressure": { // 氣壓 [cite: 19]
        "meanSeaLevelMillibars": 1007.72
    },
    "wind": { // 風 [cite: 19]
        "direction": { // 風向 [cite: 19]
            "degrees": 180,
            "cardinal": "SOUTH"
        },
        "speed": { // 風速 [cite: 20]
            "value": 13,
            "unit": "KILOMETERS_PER_HOUR" [cite: 20]
        },
        "gust": { // 陣風 [cite: 20]
            "value": 23,
            "unit": "KILOMETERS_PER_HOUR"
        }
    },
    "visibility": { // 能見度 [cite: 20]
        "distance": 16,
        "unit": "KILOMETERS"
    },
    "cloudCover": 95, // 雲量 [cite: 21]
    "currentConditionsHistory": { // 近期天氣歷史 [cite: 21]
        "temperatureChange": {
            "degrees": 0.7,
            "unit": "CELSIUS"
        },
        "maxTemperature": { // 最高溫 [cite: 21]
            "degrees": 25.5,
            "unit": "CELSIUS"
        },
        "minTemperature": { // 最低溫 [cite: 22]
            "degrees": 23.2,
            "unit": "CELSIUS" [cite: 22]
        },
        "snowQpf": {
            "quantity": 0,
            "unit": "MILLIMETERS"
        },
        "qpf": {
            "quantity": 0.0356, [cite: 23]
            "unit": "MILLIMETERS" [cite: 23]
        }
    }
}

從響應中可見,API 提供的數據極為詳盡,除了基礎的溫度 (temperature)、天氣描述 (description.text) 和圖示連結,還包括體感溫度 (feelsLikeTemperature)、降水機率與預估量 (precipitation)、風速風向 (wind)、相對濕度 (relativeHumidity) 等多項專業氣象參數。

Forecast (天氣預報) 端點

與 Current Conditions 端點類似,Forecast 端點也只需要您想查詢天氣數據地點的地理座標 。  

GET https://weather.googleapis.com/v1/forecast/days:lookup?key=YOUR_API_KEY&location.latitude=LATITUDE&location.longitude=LONGITUDE  

  • YOUR_API_KEY (必要): 您已啟用 Weather API 的 Google Maps API 金鑰 。  
  • LATITUDE (必要): 您查詢地點的緯度,例如香港為 22.302711 。  
  • LONGITUDE (必要): 您查詢地點的經度,例如香港為 114.177216 。  

預設情況下,forecast/days 端點會回傳從當前小時起算的 10 天數據 。您可以透過在請求中加入 days 參數來指定特定天數的預報,例如:https://weather.googleapis.com/v1/forecast/days:lookup?key={YOUR_API_KEY}&location.latitude=22.302711&location.longitude=114.177216&days=2 會回傳香港未來兩天的天氣預報 。  

以下是請求香港 未來兩天每日預報 (/forecast/days 加上 &days=2) 的部分 JSON 範例:

{
    "forecastDays": [ // 預報天數陣列 [cite: 31, 85]
        { // 第一天預報
            "interval": { // 時間區間 [cite: 31]
                "startTime": "2025-04-17T23:00:00Z",
                "endTime": "2025-04-18T23:00:00Z"
            },
            "displayDate": { // 顯示日期 [cite: 32]
                "year": 2025, [cite: 32]
                "month": 4,
                "day": 18
            },
            "daytimeForecast": { // 白天預報 [cite: 32, 85]
                "interval": {
                    "startTime": "2025-04-17T23:00:00Z",
                    "endTime": "2025-04-18T11:00:00Z" [cite: 33]
                },
                "weatherCondition": { // 天氣狀況 [cite: 33]
                    "iconBaseUri": "https://maps.gstatic.com/weather/v1/drizzle",
                    "description": {
                        "text": "Light rain", // 描述文字:小雨 [cite: 34]
                        "languageCode": "en"
                    },
                    "type": "LIGHT_RAIN" [cite: 35]
                },
                "relativeHumidity": 90, // 相對濕度 [cite: 35]
                "uvIndex": 4, // 紫外線指數 [cite: 35]
                "precipitation": { // 降水 [cite: 35, 85]
                    "probability": {
                        "percent": 20, [cite: 36]
                        "type": "RAIN"
                    },
                    "snowQpf": {
                        "quantity": 0,
                        "unit": "MILLIMETERS" [cite: 37]
                    },
                    "qpf": {
                        "quantity": 0.0356,
                        "unit": "MILLIMETERS" [cite: 38]
                    }
                },
                "thunderstormProbability": 40, // 雷暴機率 [cite: 38]
                "wind": { // 風 [cite: 38, 85]
                    "direction": { // 風向 [cite: 39]
                        "degrees": 165,
                        "cardinal": "SOUTH_SOUTHEAST"
                    },
                    "speed": { // 風速 [cite: 40]
                        "value": 14,
                        "unit": "KILOMETERS_PER_HOUR"
                    },
                    "gust": { // 陣風 [cite: 41]
                        "value": 24,
                        "unit": "KILOMETERS_PER_HOUR"
                    }
                },
                "cloudCover": 100, // 雲量 [cite: 41]
                "iceThickness": { // 結冰厚度 [cite: 42]
                    "thickness": 0,
                    "unit": "MILLIMETERS"
                }
            },
            "nighttimeForecast": { // 夜間預報 [cite: 42, 85]
                "interval": { [cite: 43]
                    "startTime": "2025-04-18T11:00:00Z",
                    "endTime": "2025-04-18T23:00:00Z"
                },
                "weatherCondition": {
                    "iconBaseUri": "https://maps.gstatic.com/weather/v1/drizzle",
                    "description": { [cite: 44]
                        "text": "Light rain",
                        "languageCode": "en"
                    },
                    "type": "LIGHT_RAIN" [cite: 45]
                },
                "relativeHumidity": 93, [cite: 45]
                "uvIndex": 0, [cite: 45]
                "precipitation": {
                    "probability": {
                        "percent": 20, [cite: 46]
                        "type": "RAIN"
                    },
                    "snowQpf": {
                        "quantity": 0, [cite: 47]
                        "unit": "MILLIMETERS"
                    },
                    "qpf": {
                        "quantity": 0, [cite: 48]
                        "unit": "MILLIMETERS"
                    }
                },
                "thunderstormProbability": 40, [cite: 48]
                "wind": { [cite: 49]
                    "direction": {
                        "degrees": 175,
                        "cardinal": "SOUTH"
                    },
                    "speed": { [cite: 50]
                        "value": 13,
                        "unit": "KILOMETERS_PER_HOUR"
                    },
                    "gust": { [cite: 51]
                        "value": 23,
                        "unit": "KILOMETERS_PER_HOUR"
                    }
                },
                "cloudCover": 100, [cite: 52]
                "iceThickness": {
                    "thickness": 0,
                    "unit": "MILLIMETERS"
                }
            },
            "maxTemperature": { // 最高溫 [cite: 53]
                "degrees": 25.8,
                "unit": "CELSIUS"
            },
            "minTemperature": { // 最低溫 [cite: 53]
                "degrees": 23.6,
                "unit": "CELSIUS" [cite: 54]
            },
            "feelsLikeMaxTemperature": { // 最高體感溫度 [cite: 54]
                "degrees": 28.9,
                "unit": "CELSIUS"
            },
            "feelsLikeMinTemperature": { // 最低體感溫度 [cite: 54]
                "degrees": 25.9,
                "unit": "CELSIUS" [cite: 55]
            },
            "sunEvents": { // 日出日落時間 [cite: 55]
                "sunriseTime": "2025-04-17T22:01:02.724182325Z",
                "sunsetTime": "2025-04-18T10:44:33.202524577Z"
            },
            "moonEvents": { // 月相、月出月落時間 [cite: 56]
                "moonPhase": "WANING_GIBBOUS",
                "moonriseTimes": [
                    "2025-04-18T15:30:01.703719186Z"
                ],
                "moonsetTimes": [
                    "2025-04-18T01:16:17.718137080Z" [cite: 57]
                ]
            },
            "maxHeatIndex": { // 最高酷熱指數 [cite: 57]
                "degrees": 28.9,
                "unit": "CELSIUS"
            }
        },
        { // 第二天預報 [cite: 58]
           // ... (第二天預報數據,結構類似第一天)
        }
    ],
    "timeZone": { // 時區 [cite: 84]
        "id": "Asia/Hong_Kong"
    }
}

在響應中,您可以在 forecastDays 陣列的每個條目中,找到每日(包含 daytimeForecast 白天預報和 nighttimeForecast 夜間預報)的 temperature (溫度)、description.text (描述文字) 和對應的天氣圖示,以及其他有用的資訊,如 feelsLikeTemperature (體感溫度)、precipitation (降水)、wind.direction (風向) 和 humidity (濕度) 。  

日曆小工具 (Calendar Widget)

一旦您從 Google Weather API 獲得響應,就可以透過基本的 HTML 和 Javascript,根據需要格式化 forecastDays 陣列,輕鬆製作一個日曆天氣小工具 。  

(以下為 Javascript 程式碼範例,用以更新預報顯示)

function updateForecast(forecastDays) {
  const container = document.getElementById("forecast-container"); // 取得容器元素 [cite: 86]
  const template = document.getElementById("forecast-card-template"); // 取得模板元素 [cite: 87]
  // 儲存模板內容 [cite: 87]
  const templateContent = template.content.cloneNode(true);
  // 清除現有的預報卡片 [cite: 88]
  container.innerHTML = "";
  forecastDays.forEach((day) => { // 遍歷每一天的預報 [cite: 88]
    const date = new Date(day.interval.startTime); // 取得日期物件 [cite: 88]
    // 格式化星期幾 [cite: 88]
    const dayOfWeek = new Intl.DateTimeFormat("en-US", {
      weekday: "short",
    }).format(date);
    // 格式化月日 [cite: 88]
    const monthDay = new Intl.DateTimeFormat("en-US", {
      month: "short",
      day: "numeric",
    }).format(date);
    // 複製模板 [cite: 88]
    const card = templateContent.cloneNode(true);
    // 更新日期內容 [cite: 89]
    card.querySelector(".forecast-date").textContent = `${dayOfWeek}, ${monthDay}`; [cite: 89]
    // 白天預報 [cite: 89]
    const dayTemp = card.querySelector(".day-temp");
    dayTemp.textContent = `${Math.round(day.maxTemperature.degrees)}°`; // 設定最高溫 [cite: 89]
    const dayIcon = card.querySelector(".day-temp .weather-icon"); // 取得白天圖示元素 [cite: 89]
    if (!dayIcon) { // 如果沒有圖示元素,則創建一個 [cite: 89]
      const icon = document.createElement("img");
      icon.className = "weather-icon";
      icon.src = `${day.daytimeForecast.weatherCondition.iconBaseUri}.png`; // 設定圖示來源 [cite: 89]
      icon.alt = day.daytimeForecast.weatherCondition.description.text; // 設定替代文字 [cite: 89]
      dayTemp.appendChild(icon); // 加入圖示 [cite: 89]
    } else { // 如果已有圖示元素,則更新它 [cite: 90]
      dayIcon.src = `${day.daytimeForecast.weatherCondition.iconBaseUri}.png`; [cite: 90]
      dayIcon.alt = day.daytimeForecast.weatherCondition.description.text; [cite: 90]
    }
    // 設定白天描述 [cite: 91]
    card.querySelector(".day-description").textContent = day.daytimeForecast.weatherCondition.description.text;
    // 夜間預報 [cite: 92]
    const nightTemp = card.querySelector(".night-temp");
    nightTemp.textContent = `${Math.round(day.minTemperature.degrees)}°`; // 設定最低溫 [cite: 92]
    const nightIcon = card.querySelector(".night-temp .weather-icon"); // 取得夜間圖示元素 [cite: 92]
    if (!nightIcon) { // 如果沒有圖示元素,則創建一個 [cite: 93]
      const icon = document.createElement("img");
      icon.className = "weather-icon";
      icon.src = `${day.nighttimeForecast.weatherCondition.iconBaseUri}.png`; // 設定圖示來源 [cite: 93]
      icon.alt = day.nighttimeForecast.weatherCondition.description.text; // 設定替代文字 [cite: 94]
      nightTemp.appendChild(icon); // 加入圖示 [cite: 94]
    } else { // 如果已有圖示元素,則更新它 [cite: 94]
      nightIcon.src = `${day.nighttimeForecast.weatherCondition.iconBaseUri}.png`; [cite: 94]
      nightIcon.alt = day.nighttimeForecast.weatherCondition.description.text; [cite: 95]
    }
    // 設定夜間描述 [cite: 95]
    card.querySelector(".night-description").textContent = day.nighttimeForecast.weatherCondition.description.text;
    // 將完成的卡片加入容器 [cite: 95]
    container.appendChild(card);
  });
}

Google Weather API 的費用如何?

截至本文發布時 (2025 年 4 月),Google Weather API 仍處於公開預覽 (Public Preview) 階段,在此階段無需付費即可使用。這為開發者提供了一個絕佳的評估與整合機會。

關於正式發布 (General Availability, GA) 後的定價策略,建議密切關注 Google Maps Platform 的官方公告 。如想了解更多剛灣資訊,歡迎找我們專業的 GIS 團隊諮詢。  

Google Weather API 作為 Google Maps Platform 環境與地理空間分析工具系列的重要一環,為開發者和企業提供了一種獲取高質量、高精度全球天氣數據的強大途徑。其易用性、數據的全面性以及與 Google 生態系統 (如 Places API、Routes API) 的無縫整合能力,使其成為開發天氣相關應用的理想選擇。

無論是需要即時天氣狀況、短期每小時預報,還是長達十天的每日趨勢,Weather API 都能提供可靠的數據支持。其在物流、零售、能源、農業、保險等多個行業的應用潛力巨大,有望驅動新一輪的數據驅動創新。

我們鼓勵您深入探索 Google Weather API 的功能與潛力。若您在評估或使用過程中遇到任何技術問題,或希望了解如何將 Google Maps Platform 的地理空間解決方案應用於您的具體業務場景,歡迎隨時與我們的專業技術顧問團隊聯繫!

歡迎您與我們聯絡
我們會協助您取得最佳解決方案!

歡迎您與我們聯絡
我們會協助您取得最佳解決方案!

思想科技 Master Concept
微信公众号:Master_Concept