使用 Azure 地圖服務氣象服務來要求即時和預報的氣象資料
Azure 地圖服務氣象服務是一組 RESTful API,可讓開發人員將高度動態的歷程記錄、即時和預報的氣象資料和視覺效果整合至其解決方案中。
本文示範如何要求即時和預報的氣象資料:
- 使用取得目前狀況 API 來要求即時 (目前) 氣象資料。
- 使用取得嚴重氣象警示 API 來要求嚴重氣象警示。
- 使用取得每日預報 API 來要求每日預報。
- 使用取得每小時預報 API 來要求每小時預報。
- 使用取得每分鐘預報 API 來要求每分鐘的預報。
此影片提供對 Azure 地圖服務氣象服務進行 REST 呼叫的範例。
必要條件
-
重要
取得每分鐘預報 API 需要 Gen1 (S1) 或 Gen2 定價層。
本教學課程使用 Postman 應用程式,但您可以選擇不同的 API 開發環境。
要求即時氣象資料
取得目前狀況 API 會傳回指定座標位置的詳細氣象狀況,例如,降雨、溫度和風力。 此外,也可以擷取特定位置過去 6 或 24 小時的觀察資料。 回應包含的詳細資料,例如觀測日期和時間、氣象狀況的描述、氣象圖示、降雨機率指示器旗標,以及溫度。 也會傳回 RealFeel™ 溫度和紫外線 (UV) 指數。
在此範例中,您將使用取得目前狀況 API 來擷取座標位於 Seattle, WA 的目前氣象狀況。
開啟 Postman 應用程式。 選取 [新增] 以建立要求。 在 [新建] 視窗中,選取 [HTTP 要求]。 輸入要求的 [要求名稱]。
選取建立器索引標籤上的 GET HTTP 方法,然後輸入下列 URL。 對於此要求以及本文中提及的其他要求,請將
{Your-Azure-Maps-Subscription-key}
取代為您的 Azure 地圖服務訂用帳戶金鑰。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 地圖服務取得嚴重氣象警示 API 會傳回從官方政府氣象機構和領先全球的區域性氣象警示提供者提供的嚴重氣象警示。 服務會傳回警示類型、類別、層級等詳細資料。 此服務也會傳回所要求位置作用中嚴重警示 (例如颶風、雷雨、閃電、熱浪或森林大火) 的詳細描述。 例如,物流經理可以在地圖上將嚴重氣象狀況,連同商務位置和規劃路線視覺化,並與司機和當地工作人員進一步協調。
在此範例中,您要使用取得嚴重氣象警示 API 來擷取座標位於 Cheyenne, WY 的目前氣象狀況。
注意
本範例在撰寫本文時擷取嚴重的氣象警示。 在要求的位置上,可能不再有任何嚴重的氣象警示。 若要在執行此範例時擷取實際的嚴重警示資料,您必須在不同的座標位置擷取資料。
在 Postman 應用程式中,選取 [新增] 以建立要求。 在 [新建] 視窗中,選取 [HTTP 要求]。 輸入要求的 [要求名稱]。
選取建立器索引標籤上的 GET HTTP 方法,然後輸入下列 URL。 對於此要求以及本文中提及的其他要求,請將
{Your-Azure-Maps-Subscription-key}
取代為您的 Azure 地圖服務訂用帳戶金鑰。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
來要求五天。
重要
在 S0 定價層中,您可以要求未來 1、5、10 和 15 天的每日預報。 在 Gen1 (S1) 或 Gen2 定價層中,您可以要求未來 25 天和 45 天的每日預報。
Azure 地圖服務 Gen1 定價層淘汰
Gen1 定價層現已遭取代,且將於 2026/9/15 淘汰。 Gen2 定價層會取代 Gen1 (S0 和 S1) 定價層。 如果 Azure 地圖服務帳戶已選取 Gen1 定價層,您可以在淘汰之前切換至 Gen2 定價,否則將會自動更新。 如需詳細資訊,請參閱管理 Azure 地圖服務帳戶的定價層。
在此範例中,您要使用取得每日預報 API 來擷取座標位於 Seattle, WA 的五天氣象預報。
在 Postman 應用程式中,選取 [新增] 以建立要求。 在 [新建] 視窗中,選取 [HTTP 要求]。 輸入要求的 [要求名稱]。
選取建立器索引標籤上的 GET HTTP 方法,然後輸入下列 URL。 對於此要求以及本文中提及的其他要求,請將
{Your-Azure-Maps-Subscription-key}
取代為您的 Azure 地圖服務訂用帳戶金鑰。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}
選取藍色的 [傳送] 按鈕。 回應本文包含五天的氣象預報資料。 為了簡潔起見,下方 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 小時座標位於 Seattle, WA 的每小時氣象預報。
在 Postman 應用程式中,選取 [新增] 以建立要求。 在 [新建] 視窗中,選取 [HTTP 要求]。 輸入要求的 [要求名稱]。
選取建立器索引標籤上的 GET HTTP 方法,然後輸入下列 URL。 對於此要求以及本文中提及的其他要求,請將
{Your-Azure-Maps-Subscription-key}
取代為您的 Azure 地圖服務訂用帳戶金鑰。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 來擷取座標位於 Seattle, WA 的每分鐘氣象預報。 提供未來 120 分鐘的氣象預報。 我們的查詢要求以 15 分鐘間隔提供預報,但您可以將參數調整為 1 或 5 分鐘。
在 Postman 應用程式中,選取 [新增] 以建立要求。 在 [新建] 視窗中,選取 [HTTP 要求]。 輸入要求的 [要求名稱]。
選取建立器索引標籤上的 GET HTTP 方法,然後輸入下列 URL。 對於此要求以及本文中提及的其他要求,請將
{Your-Azure-Maps-Subscription-key}
取代為您的 Azure 地圖服務訂用帳戶金鑰。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 } ] }