您可以为所有 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 值已从公制单位更改为英制单位
下表列出了可在不同单位制之间转换的天气 API 响应值。
单位换算(公制到英制) | Weather API 值 |
---|---|
摄氏度 (°C) 到华氏度 (°F) | 温度、体感温度、、热指数、风寒指数、湿球温度 |
毫米 (mm) 到英寸 (in) | 降水、QPF、冰厚度 |
公里/小时 (km/h) 到英里/小时 (mph) | 风速、阵风 |
公里 (km) 换算为英里 (mi) | 公开范围 |
currentConditions 响应示例
下表列出了 currentConditions
请求的响应正文中包含的值。以粗体蓝色显示的数值已从 METRIC
单位更改为 IMPERIAL
单位:
天气 API 功能 | 指标(默认) | IMPERIAL |
---|---|---|
当前时间 | 2025-03-06T11:08:49.126979608Z | 2025 年 3 月 6 日上午 6:08:49 |
时区 | America/New_York | America/New_York |
是否为白天 | FALSE | FALSE |
天气说明 | 大部多云 | 大部多云 |
温度 | 11.5 °C | 52.7 °F |
体感温度 | 9.9 °C | 49.7 °F |
Dew Point | 8.7 °C | 47.6 °F |
热指数 | 11.5 °C | 52.7 °F |
风寒温度 | 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 ° |
风向(基本方向) | WEST | WEST |
风速 | 14 公里/小时 | 9 英里/小时 |
阵风 | 27 公里/小时 | 17 英里/小时 |
公开范围 | 10 公里 | 6 英里 |
Cloud Cover | 65 % | 65 % |
温度变化 | 1.4 °C | 2.6 °F |
最高温度 | 13.2 °C | 55.8 °F |
最低温度 | 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