您可以為所有 Weather API 端點選擇 METRIC
或 IMPERIAL
。
如要要求單位系統,請將 units_system
設為 METRIC
或 IMPERIAL
。如未指定 units_system
,系統會預設為 METRIC
。
https://weather.googleapis.com/v1/currentConditions:lookup?key=YOUR_API_KEY&location.latitude=LATITUDE&location.longitude=LONGITUDE&units_system=IMPERIAL
天氣 API 值從公制單位改為英制單位
下表列出可轉換單位系統的 Weather API 回應值。
單位轉換 (公制轉英制) | Weather API 值 |
---|---|
攝氏 (°C) 轉華氏 (°F) | 溫度、體感溫度、露點、熱指數、風寒指數、溼球溫度 |
公釐 (mm) 到英吋 (in) | 降水、QPF、冰層厚度 |
公里/小時 (km/h) 到英里/小時 (mph) | 風速、陣風 |
公里 (km) 換算成英里 (mi) | 顯示設定 |
currentConditions 回應範例
下表列出 currentConditions
要求的回應內容中包含的值。以藍色粗體顯示的值已從「METRIC
」變更為「IMPERIAL
」單位:
天氣 API 功能 | 指標 (預設) | 英制 |
---|---|---|
目前時間 | 2025-03-06T11:08:49.126979608Z | 3/6/2025, 6:08:49 AM |
時區 | America/New_York | America/New_York |
是否為白天 | FALSE | FALSE |
天氣說明 | 多雲時陰 | 多雲時陰 |
溫度 | 11.5 °C | 11.5 °C |
體感溫度 | 9.9 °C | 49.7 °F |
露點 | 8.7 °C | 47.6 °F |
體感溫度 | 11.5 °C | 11.5 °C |
風寒 | 9.9 °C | 49.7 °F |
相對濕度 | 83 % | 83 % |
紫外線指數 | 0 | 0 |
降水機率 | 9 % | 9 % |
降水率 | 0 公釐 | 0 in |
雷雨機率 | 0 | 0 |
氣壓 | 991.47 mb | 991.47 mb |
風向 | 275 ° | 275 ° |
風向 (方位) | 西 | 西 |
風速 | 14 公里/小時 | 9 英里/小時 |
陣風 | 27 公里/小時 | 17 英里/小時 |
顯示設定 | 10 公里 | 9.6 公里 |
Cloud Cover | 65 % | 65 % |
溫度變化 | 1.4 °C | 2.6 °F |
最高溫度 | 13.2 °C | 13.2 °C |
最低溫度 | 10.1 °C | 10.1 °C |
QPF | 27.5564 公釐 | 1.0849 英吋 |
世界各地的單位制度
各國家/地區的單位制
美國以外的大多數國家/地區使用 METRIC
系統,而美國則使用 IMPERIAL
系統。有些國家/地區 (例如英國和加拿大) 則採用混合制度。如要查看使用公制單位的國家/地區地圖和完整清單,請參閱「公制化」一文。
轉換 Weather API 回應中的單位
手動轉換單位
您可以使用下列指令碼手動轉換單位:
function convert(conversionType, value) { if (value === null || value === undefined || typeof value !== 'number') { return "Invalid input"; // Handle null, undefined, or non-numeric input } switch (conversionType) { case 'C_to_F': // Celsius to Fahrenheit let convertedCtoF = (value * 9 / 5) + 32; return convertedCtoF.toFixed(2) + " °F"; case 'mm_to_in': // Millimeters to Inches let convertedMmToIn = value * 0.0393701; return convertedMmToIn.toFixed(2) + " in"; case 'km_to_mi': // Kilometers to Miles let convertedKmToMi = value * 0.621371; return convertedKmToMi.toFixed(2) + " mi"; case 'km/h_to_mph': // Kilometers per hour to Miles per hour let convertedKmHToMph = value * 0.621371; return convertedKmHToMph.toFixed(2) + " mph"; case 'millibar_to_psi': // Millibar to PSI let convertedMillibarToPsi = value * 0.0145038; return convertedMillibarToPsi.toFixed(2) + " psi"; default: return "Invalid conversion type"; } } console.log(convert('C_to_F', 10)); // Output: 50.00 °F console.log(convert('mm_to_in', 25)); // Output: 0.98 in console.log(convert('invalid_type', 5)); // Output: Invalid conversion type
使用程式庫轉換單位
您也可以使用 convert-units 等程式庫轉換單位:
// module
import convert from 'convert-units';
// or script inclusion
// <script src="https://unpkg.com/convert@5.8.0/dist/index.js" ></script>
// let convert = convert.convert
function convertWithLibrary(conversionType, value) {
if (value === null || value === undefined || typeof value !== 'number') {
return "Invalid input";
}
try {
switch (conversionType) {
case 'C_to_F':
let convertedCtoF = convert(value, 'C').to('F');
return convertedCtoF.toFixed(2) + " °F";
case 'mm_to_in':
let convertedMmToIn = convert(value, 'mm').to('in');
return convertedMmToIn.toFixed(2) + " in";
case 'km_to_mi':
let convertedKmToMi = convert(value, 'km').to('mi');
return convertedKmToMi.toFixed(2) + " mi";
case 'km/h_to_mph':
// km/h is not directly supported, so we convert km to mi
// then assume it's per hour
let convertedKmToMiValue = convert(value, 'km').to('mi');
return convertedKmToMiValue.toFixed(2) + " mph";
case 'millibar_to_psi':
// millibar is not directly supported, so convert millibar to bar, then bar to psi
let barValue = value / 1000;
let convertedMillibarToPsi = convert(barValue, 'bar').to('psi');
return convertedMillibarToPsi.toFixed(2) + " psi";
default:
return "Invalid conversion type";
}
} catch (error) {
console.error("Conversion error:", error);
return "Conversion failed";
}
}
console.log(convertWithLibrary('C_to_F', 10)); // Output: 50.00 °F
console.log(convertWithLibrary('mm_to_in', 25)); // Output: 0.98 in
console.log(convertWithLibrary('invalid_type', 5)); // Output: Invalid conversion type