| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 | 
							- using Microsoft.AspNetCore.Http;
 
- using System.Net.Http.Json;
 
- using System.Text.Json;
 
- namespace OASystem.API.OAMethodLib.AMapApi
 
- {
 
-     /// <summary>
 
-     /// 高德地图API Service
 
-     /// </summary>
 
-     public class GeocodeService
 
-     {
 
-         private readonly string _apiKey = "2f5a9ef15f598df3170811f50787166a"; // 高德API Key
 
-         private readonly HttpClient _httpClient;
 
-         /// <summary>
 
-         /// 初始化
 
-         /// </summary>
 
-         /// <param name="httpClient"></param>
 
-         public GeocodeService(HttpClient httpClient)
 
-         {
 
-             _httpClient = httpClient;
 
-         }
 
-         /// <summary>
 
-         /// 获取单个地址的经纬度
 
-         /// </summary>
 
-         /// <param name="address"></param>
 
-         /// <returns></returns>
 
-         public async Task<GeocodeResult> GetGeocodeAsync(string address)
 
-         {
 
-             if (string.IsNullOrEmpty(address)) return new GeocodeResult { Address = address, Status = "Failed", Info = "地址为空!" };
 
-             string url = $"https://restapi.amap.com/v3/geocode/geo?key={_apiKey}&address={Uri.EscapeDataString(address)}";
 
-             var response = await _httpClient.GetFromJsonAsync<JsonElement>(url);
 
-             if (response.GetProperty("status").GetString() == "1" && response.GetProperty("geocodes").GetArrayLength() > 0)
 
-             {
 
-                 var location = response.GetProperty("geocodes")[0].GetProperty("location").GetString();
 
-                 var coordinates = location.Split(',');
 
-                 return new GeocodeResult
 
-                 {
 
-                     Address = address,
 
-                     Longitude = double.Parse(coordinates[0]),
 
-                     Latitude = double.Parse(coordinates[1]),
 
-                     Status = "Success",
 
-                     Info = "OK"
 
-                 };
 
-             }
 
-             else
 
-             {
 
-                 return new GeocodeResult
 
-                 {
 
-                     Address = address,
 
-                     Status = "Failed",
 
-                     Info = response.GetProperty("info").GetString()
 
-                 };
 
-             }
 
-         }
 
-         /// <summary>
 
-         /// 批量获取多个地址的经纬度
 
-         /// </summary>
 
-         /// <param name="addresses"></param>
 
-         /// <param name="qpsLimit"></param>
 
-         /// <returns></returns>
 
-         public async Task<List<GeocodeResult>> GetGeocodesAsync(IEnumerable<string> addresses, int qpsLimit = 3)
 
-         {
 
-             var results = new List<GeocodeResult>();
 
-             for (int i = 0; i < addresses.Count(); i += qpsLimit)
 
-             {
 
-                 // 每次处理 qpsLimit 个地址
 
-                 var batch = addresses.Skip(i).Take(qpsLimit);
 
-                 // 创建并启动任务
 
-                 var tasks = batch.Select(GetGeocodeAsync).ToList();
 
-                 var batchResults = await Task.WhenAll(tasks);
 
-                 // 收集结果
 
-                 results.AddRange(batchResults);
 
-                 // 如果还有剩余请求,延迟一秒
 
-                 if (i + qpsLimit < addresses.Count())
 
-                 {
 
-                     await Task.Delay(1000);
 
-                 }
 
-             }
 
-             return results;
 
-         }
 
-         public class GeocodeResult
 
-         {
 
-             public string Address { get; set; }
 
-             public double Latitude { get; set; }
 
-             public double Longitude { get; set; }
 
-             public string Status { get; set; }
 
-             public string Info { get; set; }
 
-         }
 
-     }
 
- }
 
 
  |