https://ip-api.com/json/ 或淘騰 IP 庫,解析 regionName (省)、city (市)。GET https://devapi.qweather.com/v7/weather/now?location=101010100&key=YOUR_KEY
解析 JSON 取得 temp, text, windDir, humidity, aqi 等。Geolocator 獲取經緯度。using System.Net.Http;
using Newtonsoft.Json.Linq;
public async Task GetWeatherAsync(string city) {
// 1. 獲取城市 ID (以和風為例,需先調用城市查詢介面)
// 2. 查詢即時天氣
using (var client = new HttpClient()) {
string url = $"https://devapi.qweather.com/v7/weather/now?location={cityId}&key={KEY}";
string json = await client.GetStringAsync(url);
JObject jo = JObject.Parse(json);
return new WeatherInfo {
Temperature = jo["now"]["temp"].ToString(),
Condition = jo["now"]["text"].ToString(),
Wind = jo["now"]["windDir"].ToString(),
Humidity = jo["now"]["humidity"].ToString(),
AQI = jo["now"]["aqi"]?.ToString()
};
}
}
完整範例:C# 調用天氣 API 實現