你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
使用 Azure Maps 天气服务请求实时和预测的天气数据
Azure Maps 天气服务是一组 RESTful API,开发人员可使用它们将高度动态的历史、实时和预测天气数据和可视化效果集成到其解决方案中。
本文演示如何请求实时和预测的天气数据:
- 使用获取当前天气 API 请求实时(当前)天气数据。
- 使用获取恶劣天气警报 API 请求恶劣天气警报。
- 使用获取每日预测 API 请求每日预测天气。
- 使用获取每小时预测 API 请求每小时预测天气。
- 使用获取每分钟预测 API 请求每分钟预测天气。
此视频还有示例来展示如何对 Azure Maps 天气服务进行 REST 调用。
先决条件
-
重要
获取每分钟预测 API 需要 Gen1 (S1) 或 Gen2 定价层。
本教程使用 Postman 应用,但你也可以选择其他 API 开发环境。
请求实时天气数据
获取当前天气 API 会返回给定坐标位置的详细天气状况,例如降水量、温度和风速。 此外,还可检索过去 6 或 24 小时内特定位置的观测结果。 响应包括观测日期和时间、天气状况的说明、天气图标、降水量指示标志和温度等详细信息。 还会返回 RealFeel™ 温度和紫外线 (UV) 指数。
在本示例中,你要使用获取当前天气 API 来检索华盛顿州西雅图坐标处的当前天气状况。
打开 Postman 应用。 选择“新建”以创建请求。 在“新建”窗口中,选择“HTTP 请求” 。 在“请求名称”中,输入请求名称。
在生成器选项卡中选择“GET”HTTP 方法,然后输入以下 URL。 对于此请求和本文中提到的其他请求,请将
{Your-Azure-Maps-Subscription-key}
替换为你的 Azure Maps 订阅密钥。https://atlas.microsoft.com/weather/currentConditions/json?api-version=1.0&query=47.60357,-122.32945&subscription-key={Your-Azure-Maps-Subscription-key}
选择蓝色的“发送”按钮。 响应正文包含当前天气信息。
{ "results": [ { "dateTime": "2020-10-19T20:39:00+00:00", "phrase": "Cloudy", "iconCode": 7, "hasPrecipitation": false, "isDayTime": true, "temperature": { "value": 12.4, "unit": "C", "unitType": 17 }, "realFeelTemperature": { "value": 13.7, "unit": "C", "unitType": 17 }, "realFeelTemperatureShade": { "value": 13.7, "unit": "C", "unitType": 17 }, "relativeHumidity": 87, "dewPoint": { "value": 10.3, "unit": "C", "unitType": 17 }, "wind": { "direction": { "degrees": 23.0, "localizedDescription": "NNE" }, "speed": { "value": 4.5, "unit": "km/h", "unitType": 7 } }, "windGust": { "speed": { "value": 9.0, "unit": "km/h", "unitType": 7 } }, "uvIndex": 1, "uvIndexPhrase": "Low", "visibility": { "value": 9.7, "unit": "km", "unitType": 6 }, "obstructionsToVisibility": "", "cloudCover": 100, "ceiling": { "value": 1494.0, "unit": "m", "unitType": 5 }, "pressure": { "value": 1021.2, "unit": "mb", "unitType": 14 }, "pressureTendency": { "localizedDescription": "Steady", "code": "S" }, "past24HourTemperatureDeparture": { "value": -2.1, "unit": "C", "unitType": 17 }, "apparentTemperature": { "value": 15.0, "unit": "C", "unitType": 17 }, "windChillTemperature": { "value": 12.2, "unit": "C", "unitType": 17 }, "wetBulbTemperature": { "value": 11.3, "unit": "C", "unitType": 17 }, "precipitationSummary": { "pastHour": { "value": 0.0, "unit": "mm", "unitType": 3 }, "past3Hours": { "value": 0.0, "unit": "mm", "unitType": 3 }, "past6Hours": { "value": 0.0, "unit": "mm", "unitType": 3 }, "past9Hours": { "value": 0.0, "unit": "mm", "unitType": 3 }, "past12Hours": { "value": 0.0, "unit": "mm", "unitType": 3 }, "past18Hours": { "value": 0.0, "unit": "mm", "unitType": 3 }, "past24Hours": { "value": 0.4, "unit": "mm", "unitType": 3 } }, "temperatureSummary": { "past6Hours": { "minimum": { "value": 12.2, "unit": "C", "unitType": 17 }, "maximum": { "value": 14.0, "unit": "C", "unitType": 17 } }, "past12Hours": { "minimum": { "value": 12.2, "unit": "C", "unitType": 17 }, "maximum": { "value": 14.0, "unit": "C", "unitType": 17 } }, "past24Hours": { "minimum": { "value": 12.2, "unit": "C", "unitType": 17 }, "maximum": { "value": 15.6, "unit": "C", "unitType": 17 } } } } ] }
请求恶劣天气警报
Azure Maps 获取恶劣天气警报 API 会返回来自官方政府气象机构以及领先的全球到区域天气警报提供商提供的全球范围的恶劣天气警报。 该服务会返回警报类型、类别、级别等详细信息。 该服务也会返回请求位置的待处理恶劣警报(如飓风、雷暴、闪电、热浪或森林火灾)的详细说明。 例如,除了业务位置和计划路线以外,后勤经理还可在地图上直观显示恶劣天气状况,并与司机和当地工人进一步协调。
在本示例中,你要使用获取恶劣天气警报 API 来检索怀俄明州夏安坐标处的当前天气状况。
注意
此示例将检索撰写本文时的恶劣天气警报。 请求的位置可能不再有任何恶劣天气警报。 若要在运行此示例时检索实际的恶劣天气警报数据,需要在不同的坐标位置检索数据。
在 Postman 应用中,选择“新建”以创建请求。 在“新建”窗口中,选择“HTTP 请求” 。 在“请求名称”中,输入请求名称。
在生成器选项卡中选择“GET”HTTP 方法,然后输入以下 URL。 对于此请求和本文中提到的其他请求,请将
{Your-Azure-Maps-Subscription-key}
替换为你的 Azure Maps 订阅密钥。https://atlas.microsoft.com/weather/severe/alerts/json?api-version=1.0&query=41.161079,-104.805450&subscription-key={Your-Azure-Maps-Subscription-key}
选择蓝色的“发送”按钮。 如果没有恶劣天气警报,响应正文会包含一个空的
results[]
数组。 如果出现恶劣天气警报,响应正文将包含与下面的 JSON 响应类似的内容:{ "results": [ { "countryCode": "US", "alertId": 2194734, "description": { "localized": "Red Flag Warning", "english": "Red Flag Warning" }, "category": "FIRE", "priority": 54, "source": "U.S. National Weather Service", "sourceId": 2, "alertAreas": [ { "name": "Platte/Goshen/Central and Eastern Laramie", "summary": "Red Flag Warning in effect until 7:00 PM MDT. Source: U.S. National Weather Service", "startTime": "2020-10-05T15:00:00+00:00", "endTime": "2020-10-06T01:00:00+00:00", "latestStatus": { "localized": "Continue", "english": "Continue" }, "alertDetails": "...RED FLAG WARNING REMAINS IN EFFECT FROM 9 AM THIS MORNING TO\n7 PM MDT THIS EVENING FOR STRONG GUSTY WINDS AND LOW HUMIDITY...\n\n* WHERE...Fire weather zones 303, 304, 305, 306, 307, 308, 309,\n and 310 in southeast Wyoming. Fire weather zone 313 in Nebraska.\n\n* WIND...West to northwest 15 to 30 MPH with gusts around 40 MPH.\n\n* HUMIDITY...10 to 15 percent.\n\n* IMPACTS...Any fires that develop will likely spread rapidly.\n Outdoor burning is not recommended.\n\nPRECAUTIONARY/PREPAREDNESS ACTIONS...\n\nA Red Flag Warning means that critical fire weather conditions\nare either occurring now...or will shortly. A combination of\nstrong winds...low relative humidity...and warm temperatures can\ncontribute to extreme fire behavior.\n\n&&", "alertDetailsLanguageCode": "en" } ] },... ] }
请求每日天气预测数据
获取每日预测 API 会返回详细的每日天气预测,例如温度和风速。 请求可指定返回的天数:给定坐标位置的 1、5、10、15、25 或 45 天预测。 响应包括温度、风速、降水量、空气质量和 UV 指数等详细信息。 在此示例中,我们通过设置 duration=5
来请求 5 天的预测。
重要
在 S0 定价层中,你可请求未来 1、5、10 和 15 天的每日预测。 在 Gen1 (S1) 或 Gen2 定价层中,可以请求未来 25 天和 45 天的每日预测。
Azure Maps Gen1 定价层停用
Gen1 定价层现已弃用,并将于 2026 年 9 月 15 日开始停用。 Gen2 定价层将取代 Gen1(S0 和 S1)定价层。 如果你的 Azure Maps 帐户选择了 Gen1 定价层,你可以在停用之前切换到 Gen2 定价层,否则它会自动更新。 有关详细信息,请参阅管理 Azure Maps 帐户的定价层。
在此示例中,你要使用获取每日预测 API 来检索华盛顿州西雅图坐标处的 5 日天气预测。
在 Postman 应用中,选择“新建”以创建请求。 在“新建”窗口中,选择“HTTP 请求” 。 在“请求名称”中,输入请求名称。
在生成器选项卡中选择“GET”HTTP 方法,然后输入以下 URL。 对于此请求和本文中提到的其他请求,请将
{Your-Azure-Maps-Subscription-key}
替换为你的 Azure Maps 订阅密钥。https://atlas.microsoft.com/weather/forecast/daily/json?api-version=1.0&query=47.60357,-122.32945&duration=5&subscription-key={Your-Azure-Maps-Subscription-key}
选择蓝色的“发送”按钮。 响应正文包含 5 日天气预测数据。 为了简洁起见,以下 JSON 响应显示第一天的预测。
{ "summary": { "startDate": "2020-10-18T17:00:00+00:00", "endDate": "2020-10-19T23:00:00+00:00", "severity": 2, "phrase": "Snow, mixed with rain at times continuing through Monday evening and a storm total of 3-6 cm", "category": "snow/rain" }, "forecasts": [ { "date": "2020-10-19T04:00:00+00:00", "temperature": { "minimum": { "value": -1.1, "unit": "C", "unitType": 17 }, "maximum": { "value": 1.3, "unit": "C", "unitType": 17 } }, "realFeelTemperature": { "minimum": { "value": -6.0, "unit": "C", "unitType": 17 }, "maximum": { "value": 0.5, "unit": "C", "unitType": 17 } }, "realFeelTemperatureShade": { "minimum": { "value": -6.0, "unit": "C", "unitType": 17 }, "maximum": { "value": 0.7, "unit": "C", "unitType": 17 } }, "hoursOfSun": 1.8, "degreeDaySummary": { "heating": { "value": 18.0, "unit": "C", "unitType": 17 }, "cooling": { "value": 0.0, "unit": "C", "unitType": 17 } }, "airAndPollen": [ { "name": "AirQuality", "value": 23, "category": "Good", "categoryValue": 1, "type": "Ozone" }, { "name": "Grass", "value": 0, "category": "Low", "categoryValue": 1 }, { "name": "Mold", "value": 0, "category": "Low", "categoryValue": 1 }, { "name": "Ragweed", "value": 0, "category": "Low", "categoryValue": 1 }, { "name": "Tree", "value": 0, "category": "Low", "categoryValue": 1 }, { "name": "UVIndex", "value": 0, "category": "Low", "categoryValue": 1 } ], "day": { "iconCode": 22, "iconPhrase": "Snow", "hasPrecipitation": true, "precipitationType": "Mixed", "precipitationIntensity": "Light", "shortPhrase": "Chilly with snow, 2-4 cm", "longPhrase": "Chilly with snow, accumulating an additional 2-4 cm", "precipitationProbability": 90, "thunderstormProbability": 0, "rainProbability": 54, "snowProbability": 85, "iceProbability": 8, "wind": { "direction": { "degrees": 36.0, "localizedDescription": "NE" }, "speed": { "value": 9.3, "unit": "km/h", "unitType": 7 } }, "windGust": { "direction": { "degrees": 70.0, "localizedDescription": "ENE" }, "speed": { "value": 25.9, "unit": "km/h", "unitType": 7 } }, "totalLiquid": { "value": 4.3, "unit": "mm", "unitType": 3 }, "rain": { "value": 0.5, "unit": "mm", "unitType": 3 }, "snow": { "value": 2.72, "unit": "cm", "unitType": 4 }, "ice": { "value": 0.0, "unit": "mm", "unitType": 3 }, "hoursOfPrecipitation": 9.0, "hoursOfRain": 1.0, "hoursOfSnow": 9.0, "hoursOfIce": 0.0, "cloudCover": 96 }, "night": { "iconCode": 29, "iconPhrase": "Rain and snow", "hasPrecipitation": true, "precipitationType": "Mixed", "precipitationIntensity": "Light", "shortPhrase": "Showers of rain and snow", "longPhrase": "A couple of showers of rain or snow this evening; otherwise, cloudy; storm total snowfall 1-3 cm", "precipitationProbability": 65, "thunderstormProbability": 0, "rainProbability": 60, "snowProbability": 54, "iceProbability": 4, "wind": { "direction": { "degrees": 16.0, "localizedDescription": "NNE" }, "speed": { "value": 16.7, "unit": "km/h", "unitType": 7 } }, "windGust": { "direction": { "degrees": 1.0, "localizedDescription": "N" }, "speed": { "value": 35.2, "unit": "km/h", "unitType": 7 } }, "totalLiquid": { "value": 4.3, "unit": "mm", "unitType": 3 }, "rain": { "value": 3.0, "unit": "mm", "unitType": 3 }, "snow": { "value": 0.79, "unit": "cm", "unitType": 4 }, "ice": { "value": 0.0, "unit": "mm", "unitType": 3 }, "hoursOfPrecipitation": 4.0, "hoursOfRain": 1.0, "hoursOfSnow": 3.0, "hoursOfIce": 0.0, "cloudCover": 94 }, "sources": [ "AccuWeather" ] },... ] }
请求每小时天气预测数据
获取每小时预测 API 会按小时返回给定坐标位置未来 1、12、24 小时(1 天)、72 小时(3 天)、120 小时(5 天)和 240 小时(10 天)的详细天气预测。 此 API 会返回温度、湿度、风速、降雨量和 UV 指数等详细信息。
重要
在 Gen1 (S0) 定价层中,可请求未来 1、12、24 小时(1 天)和 72 小时(3 天)的每小时预测。 在 Gen1 (S1) 或 Gen2 定价层中,可请求未来 120 小时(5 天)和 240 小时(10 天)的每小时预测。
在此示例中,你要使用获取每小时预测 API 来检索华盛顿州西雅图坐标处未来 12 个小时的每小时天气预测。
在 Postman 应用中,选择“新建”以创建请求。 在“新建”窗口中,选择“HTTP 请求” 。 在“请求名称”中,输入请求名称。
在生成器选项卡中选择“GET”HTTP 方法,然后输入以下 URL。 对于此请求和本文中提到的其他请求,请将
{Your-Azure-Maps-Subscription-key}
替换为你的 Azure Maps 订阅密钥。https://atlas.microsoft.com/weather/forecast/hourly/json?api-version=1.0&query=47.60357,-122.32945&duration=12&subscription-key={Your-Azure-Maps-Subscription-key}
选择蓝色的“发送”按钮。 响应正文包含未来 12 小时的天气预测数据。 为了简洁起见,以下 JSON 响应显示第一个小时的预测。
{ "forecasts": [ { "date": "2020-10-19T21:00:00+00:00", "iconCode": 12, "iconPhrase": "Showers", "hasPrecipitation": true, "precipitationType": "Rain", "precipitationIntensity": "Light", "isDaylight": true, "temperature": { "value": 14.7, "unit": "C", "unitType": 17 }, "realFeelTemperature": { "value": 13.3, "unit": "C", "unitType": 17 }, "wetBulbTemperature": { "value": 12.0, "unit": "C", "unitType": 17 }, "dewPoint": { "value": 9.5, "unit": "C", "unitType": 17 }, "wind": { "direction": { "degrees": 242.0, "localizedDescription": "WSW" }, "speed": { "value": 9.3, "unit": "km/h", "unitType": 7 } }, "windGust": { "speed": { "value": 14.8, "unit": "km/h", "unitType": 7 } }, "relativeHumidity": 71, "visibility": { "value": 9.7, "unit": "km", "unitType": 6 }, "cloudCover": 100, "ceiling": { "value": 1128.0, "unit": "m", "unitType": 5 }, "uvIndex": 1, "uvIndexPhrase": "Low", "precipitationProbability": 51, "rainProbability": 51, "snowProbability": 0, "iceProbability": 0, "totalLiquid": { "value": 0.3, "unit": "mm", "unitType": 3 }, "rain": { "value": 0.3, "unit": "mm", "unitType": 3 }, "snow": { "value": 0.0, "unit": "cm", "unitType": 4 }, "ice": { "value": 0.0, "unit": "mm", "unitType": 3 } }... ] }
请求每分钟天气预测数据
获取每分钟预测 API 会返回给定位置未来 120 分钟的每分钟预测。 用户可以 1、5 和 15 分钟的间隔请求天气预测。 响应包括降水类型(包括雨、雪或雨夹雪)、开始时间和降水强度值 (dBZ) 等详细信息。
在此示例中,你要使用获取每分钟预测 API 来检索华盛顿州西雅图坐标处的每分钟天气预测。 将给出未来 120 分钟的天气预测。 查询请求按 15 分钟的间隔提供预测,但你可将参数调整为 1 或 5 分钟。
在 Postman 应用中,选择“新建”以创建请求。 在“新建”窗口中,选择“HTTP 请求” 。 在“请求名称”中,输入请求名称。
在生成器选项卡中选择“GET”HTTP 方法,然后输入以下 URL。 对于此请求和本文中提到的其他请求,请将
{Your-Azure-Maps-Subscription-key}
替换为你的 Azure Maps 订阅密钥。https://atlas.microsoft.com/weather/forecast/minute/json?api-version=1.0&query=47.60357,-122.32945&interval=15&subscription-key={Your-Azure-Maps-Subscription-key}
选择蓝色的“发送”按钮。 响应正文包含未来 120 分钟的天气预测数据,时间间隔为 15 分钟。
{ "summary": { "briefPhrase60": "No precipitation for at least 60 min", "shortPhrase": "No precip for 120 min", "briefPhrase": "No precipitation for at least 120 min", "longPhrase": "No precipitation for at least 120 min", "iconCode": 7 }, "intervalSummaries": [ { "startMinute": 0, "endMinute": 119, "totalMinutes": 120, "shortPhrase": "No precip for %MINUTE_VALUE min", "briefPhrase": "No precipitation for at least %MINUTE_VALUE min", "longPhrase": "No precipitation for at least %MINUTE_VALUE min", "iconCode": 7 } ], "intervals": [ { "startTime": "2020-10-19T20:51:00+00:00", "minute": 0, "dbz": 0.0, "shortPhrase": "No Precipitation", "iconCode": 7, "cloudCover": 100 }, { "startTime": "2020-10-19T21:06:00+00:00", "minute": 15, "dbz": 0.0, "shortPhrase": "No Precipitation", "iconCode": 7, "cloudCover": 100 }, { "startTime": "2020-10-19T21:21:00+00:00", "minute": 30, "dbz": 0.0, "shortPhrase": "No Precipitation", "iconCode": 7, "cloudCover": 100 }, { "startTime": "2020-10-19T21:36:00+00:00", "minute": 45, "dbz": 0.0, "shortPhrase": "No Precipitation", "iconCode": 7, "cloudCover": 100 }, { "startTime": "2020-10-19T21:51:00+00:00", "minute": 60, "dbz": 0.0, "shortPhrase": "No Precipitation", "iconCode": 7, "cloudCover": 100 }, { "startTime": "2020-10-19T22:06:00+00:00", "minute": 75, "dbz": 0.0, "shortPhrase": "No Precipitation", "iconCode": 7, "cloudCover": 100 }, { "startTime": "2020-10-19T22:21:00+00:00", "minute": 90, "dbz": 0.0, "shortPhrase": "No Precipitation", "iconCode": 7, "cloudCover": 100 }, { "startTime": "2020-10-19T22:36:00+00:00", "minute": 105, "dbz": 0.0, "shortPhrase": "No Precipitation", "iconCode": 7, "cloudCover": 100 } ] }