using log4net; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Configuration; using System.Text; using GpsModels; namespace GPSBusiness.BaiDu { public class BaiduGpsServercs { public static ILog log = LogManager.GetLogger(""); private static string BaiduApiUrl = ConfigurationSettings.AppSettings["BaiduApiUrl"]; private static string BaiduAk = ConfigurationSettings.AppSettings["BaiduAk"]; /// /// 批量转换百度坐标(单次最大100) /// public static List Changes100(List list) { try { var reList = new List(); string url = BaiduApiUrl + "/geoconv/v1/?coords="; foreach (var item in list) { url += item.Longitude + "," + item.Latitude + ";"; } url = url.Remove(url.Length - 1) + "&from=1&to=5&ak=" + BaiduAk; System.Net.WebRequest wReq = System.Net.WebRequest.Create(url); System.Net.WebResponse wResp = wReq.GetResponse(); System.IO.Stream respStream = wResp.GetResponseStream(); using (System.IO.StreamReader reader = new System.IO.StreamReader(respStream, Encoding.UTF8)) { string value = reader.ReadToEnd(); if (!string.IsNullOrEmpty(value)) { BaiduApiResult result = JsonConvert.DeserializeObject(value); if (result.status == 0) { List xy = JsonConvert.DeserializeObject>(result.result.ToString()); for (int i = 0; i < xy.Count; i++) { if (xy[i] != null) { list[i].Longitude = xy[i].x; list[i].Latitude = xy[i].y; reList.Add(list[i]); } } } } } return reList; } catch (Exception e) { log.Error("百度坐标转换 Exception:" + e.Message + e.StackTrace); return null; } } /// /// 批量坐标转地址(单次最大20) /// public static List AddressChanges20(List list) { try { var reList = new List(); string url = BaiduApiUrl + "/geocoder/v2/?callback=renderReverse&location="; var count = 0; var multy = 0; foreach (var item in list) { url += item.Latitude + "," + item.Longitude + "|"; count++; if (count % 20 == 0) { url = url.Remove(url.Length - 1) + "&batch=true&pois=0&extensions_road=true&extensions_town=true&latest_admin=1&output=json&pois=1&ak=" + BaiduAk; System.Net.WebRequest wReq = System.Net.WebRequest.Create(url); System.Net.WebResponse wResp = wReq.GetResponse(); System.IO.Stream respStream = wResp.GetResponseStream(); using (System.IO.StreamReader reader = new System.IO.StreamReader(respStream, Encoding.UTF8)) { string value = reader.ReadToEnd(); if (!string.IsNullOrEmpty(value)) { BaiduAddressApiResult result = JsonConvert.DeserializeObject(value); if (result.status == 0) { List add = JsonConvert.DeserializeObject>(result.areas.ToString()); for (int i = 0; i < add.Count; i++) { if (add[i] != null) { list[i + multy*20].Address = add[i].city + add[i].district + add[i].town; reList.Add(list[i]); } } } } } url = BaiduApiUrl + "/geocoder/v2/?callback=renderReverse&location="; } } return reList; } catch (Exception e) { log.Error("百度坐标转换 Exception:" + e.Message + e.StackTrace); return list; } } } /// /// ApiResult /// public class BaiduApiResult { public int status { get; set; } public string message { get; set; } public object result { get; set; } } /// /// 坐标 /// public class coordinate { /// /// 纬度 /// public decimal x { get; set; } /// /// 经度 /// public decimal y { get; set; } } /// /// BaiduAddressApiResult /// public class BaiduAddressApiResult { public int status { get; set; } public object areas { get; set; } } /// /// 坐标 /// public class addresss { public string country { get; set; } public string province { get; set; } public string city { get; set; } public string district { get; set; } public string town { get; set; } } }