|
| 1 | +using QWeatherAPI.Result.GeoAPI.CityLookup; |
| 2 | +using QWeatherAPI.Tools.Net.Http; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Linq; |
| 6 | +using System.Text; |
| 7 | +using System.Threading.Tasks; |
| 8 | + |
| 9 | +namespace QWeatherAPI |
| 10 | +{ |
| 11 | + public class GeoAPI |
| 12 | + { |
| 13 | + #region 获取城市地理信息方法 |
| 14 | + /// <summary> |
| 15 | + /// 通过地区名称获取 LocationID 和 经纬度。 |
| 16 | + /// </summary> |
| 17 | + /// <param name="location">地区名称,支持绝大多数语言。</param> |
| 18 | + /// <param name="key">和风天气 API 密钥。</param> |
| 19 | + /// <param name="adm">城市的上级行政区划。</param> |
| 20 | + /// <param name="range">搜索限定范围。国家代码需使用 ISO 3166 所定义的国家代码,默认为 10。详情请参考:https://www.iso.org/obp/ui/</param> |
| 21 | + /// <param name="limit">地理位置 API 返回数据限制个数。</param> |
| 22 | + /// <param name="lang">返回数据语言,默认采用中文。详情请参考:https://dev.qweather.com/docs/resource/language/</param> |
| 23 | + /// <returns>城市信息 API 返回数据。</returns> |
| 24 | + public static async Task<GeoResult> GetGeoAsync(string location, string key, string adm, string range = "world", int limit = 10, string lang = "zh") |
| 25 | + { |
| 26 | + range = range.ToLower(); |
| 27 | + string jsonData = await WebRequests.GetRequestAsync($"https://geoapi.qweather.com/v2/city/lookup?location={location}&number={limit}&adm={adm}&range={range}&lang={lang}&key={key}"); |
| 28 | + return new GeoResult(jsonData); |
| 29 | + } |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// 通过地区名称获取 LocationID 和 经纬度。 |
| 33 | + /// </summary> |
| 34 | + /// <param name="location">地区名称,支持绝大多数语言。</param> |
| 35 | + /// <param name="key">和风天气 API 密钥。</param> |
| 36 | + /// <param name="range">搜索限定范围。国家代码需使用 ISO 3166 所定义的国家代码,默认为 10。详情请参考:https://www.iso.org/obp/ui/</param> |
| 37 | + /// <param name="limit">地理位置 API 返回数据限制个数。</param> |
| 38 | + /// <param name="lang">返回数据语言,默认采用中文。详情请参考:https://dev.qweather.com/docs/resource/language/</param> |
| 39 | + /// <returns>城市信息 API 返回数据。</returns> |
| 40 | + public static async Task<GeoResult> GetGeoAsync(string location, string key, string range = "world", int limit = 10, string lang = "zh") |
| 41 | + { |
| 42 | + range = range.ToLower(); |
| 43 | + string jsonData = await WebRequests.GetRequestAsync($"https://geoapi.qweather.com/v2/city/lookup?location={location}&number={limit}&range={range}&lang={lang}&key={key}"); |
| 44 | + return new GeoResult(jsonData); |
| 45 | + } |
| 46 | + #endregion |
| 47 | + } |
| 48 | +} |
0 commit comments