Logo
Section 1 / 2

当前天气状况

学习如何获取当前天气状况数据。

导入

当前天气状况

学习如何获取当前天气状况数据。

1http://dataservice.accuweather.com/currentconditions/v1/{locationKey}
1http://dataservice.accuweather.com/currentconditions/v1/topcities/{group}
参数类型分类描述
API_KEY字符串必需这是我们的API密钥。这是一个查询参数。
locationKey整数必需这是位置的键。仅用于特定位置端点的路径参数。
group整数必需这是城市的数量。允许的值为50、100和150。仅用于热门城市端点的路径参数。
language字符串可选返回结果的语言。默认值为 en-us。这是一个查询参数。
details布尔值可选设置为 true 以在响应中包含完整详情,否则设置为 false。默认值为 false。仅用于特定位置端点的查询参数。
1// 导入库 2import fetch from "node-fetch"; 3 4// 定义API密钥 5const API_KEY = '{{API_KEY}}'; 6 7// 定义路径参数 8const locationKey = 351409; 9const group = 50; 10 11// 定义端点URL 12const urlSpecificLoc = new URL(`http://dataservice.accuweather.com/currentconditions/v1/${locationKey.toString()}`); 13const urlTopCities = new URL(`http://dataservice.accuweather.com/currentconditions/v1/topcities/${group.toString()}`); 14 15// 定义头部参数 16const headerParameters = { 17 contentType: 'application/json', 18}; 19 20// 定义查询参数 21const queryParamsSpecificLoc = new URLSearchParams({ 22 apikey: API_KEY, 23}); 24 25const queryParamsTopCities = new URLSearchParams({ 26 apikey: API_KEY, 27}); 28 29// 设置API调用选项 30const options = { 31 method: 'GET', 32 headers: headerParameters, 33}; 34 35// 函数:发起API调用 36async function getConditions(endpointUrl, queryParameters) { 37 try { 38 endpointUrl.search = queryParameters; 39 const response = await fetch(endpointUrl, options); 40 41 // 自定义函数:打印API响应 42 printResponse(response); 43 } catch (error) { 44 // 自定义函数:打印错误信息 45 printError(error); 46 } 47} 48 49async function getCurrentConditions() { 50 await getConditions(urlSpecificLoc, queryParamsSpecificLoc); 51 await getConditions(urlTopCities, queryParamsTopCities); 52} 53 54// 调用函数发起API调用 55getCurrentConditions();

特定位置端点

参数类型描述
HasPrecipitation布尔值表示是否有降水。
IsDayTime布尔值表示是否为白天。
Temperature对象温度值,包含华氏度和摄氏度两种单位(Imperial / Metric)。
RealFeelTemperature对象“体感温度”,单位为华氏度。
Wind对象包含风速和风向信息。
RelativeHumidity整数湿度值。
UVIndex整数紫外线辐射强度指标。该值可能为空。
Visibility对象包含能见度信息。
Link字符串AccuWeather网站上详细天气预报的链接。
MobileLink字符串AccuWeather移动网站上详细天气预报的链接。

热门城市端点

参数类型描述
Key字符串城市的唯一位置键。
LocalizedName字符串以请求语言显示的位置名称,默认语言为 en-us。
Country对象包含城市所属国家的信息,包括国家代码、本地化名称和英文名称。
Timezone对象包含城市所在时区的信息。
Geoposition对象包含城市地理坐标信息。
IsDayTime布尔值表示是否为白天。
Temperature对象提供华氏度和摄氏度的温度值。
HasPrecipitation布尔值表示是否有降水。
Link字符串AccuWeather网站上详细天气预报的链接。
MobileLink字符串AccuWeather移动网站上详细天气预报的链接。

预测 API

历史天气状况