百度根据ip获取gps地址街道中文名
百度通过ip地址获取gps和中文城市街道地址:
#region //getGPS地址,根据网络IP地址获取GPS位置和中文县区街道地址 /// <summary> /// 根据网络IP地址获取GPS位置和中文县区街道地址,参考百度的: /// http://lbsyun.baidu.com/apiconsole/key /// https://www.cnblogs.com/zhaotianff/p/5988177.html /// 应用AK:1111111111111111,自己去百度申请 /// </summary> /// <param name="sBaiduKey"></param> /// <returns></returns> public static iDefine.ChuLiJieGuo getGPS地址(string sBaiduKey = "", string s坐标格式 = "bd09ll") { iDefine.ChuLiJieGuo result = new iPublic.iDefine.ChuLiJieGuo(), jg = null; string s = "", sUrl = "https://api.map.baidu.com/location/ip?ak=去申请&coor=bd09ll"; try { if (string.IsNullOrEmpty(sBaiduKey)) sBaiduKey = iDefine.SysConfig.sBaiduKey; //自己申请的key if (string.IsNullOrEmpty(s坐标格式)) s坐标格式 = "bd09ll"; sUrl = "https://api.map.baidu.com/location/ip?ak=" + sBaiduKey + "&coor=" + s坐标格式; //读取 if ((jg = httpGet(sUrl, ref s)).error != 0) throw new Exception("获取GPS地址通信错误:" + jg.error.ToString() + "," + jg.message); //分析返回值,可以从这里看json解析:https://www.bejson.com jsonBaiduGPS地址 baiduGps = JsonConvert.DeserializeObject<jsonBaiduGPS地址>(s); if (baiduGps == null) throw new Exception("解析百度返回值出错:" + s); //完成 result.error = 0; result.data = baiduGps; result.message = "ok"; } catch (Exception x) { if (result.error <= 0) result.error = 100; result.message = x.Message; } return result; } #endregion /// <summary> /// baidu返回的json /// </summary> public class jsonBaiduGPS地址 { /*分析返回值,可以从这里看json解析:https://www.bejson.com { "address":"CN|\u5c71\u4e1c|\u6f4d\u574a|None|CHINANET|0|0", "content":{ "address":"\u5c71\u4e1c\u7701\u6f4d\u574a\u5e02", "address_detail":{ "city":"\u6f4d\u574a\u5e02","city_code":287,"district":"", "province":"\u5c71\u4e1c\u7701","street":"","street_number":""}, "point":{ "x":"100.14263382","y":"37.81611487"} },"status":0} */ public string address = ""; //CN|山东|潍坊|None|CHINANET|0|0 public _content content = null; public class _content { public string address = ""; //山东省潍坊市 public _address_detail address_detail = null; public class _address_detail { public string city = ""; //潍坊市 public string city_code = ""; //287 public string district = ""; // public string province = ""; //山东省 public string street = ""; // public string street_number = ""; } public _point point = null; public class _point { public string x = ""; //106.54263382 public string y = ""; //37.61311487 } } public void decode() { //反解出来中文 address = HttpUtility.UrlDecode(address); content.address = HttpUtility.UrlDecode(content.address); content.address_detail.city = HttpUtility.UrlDecode(content.address_detail.city); content.address_detail.province = HttpUtility.UrlDecode(content.address_detail.province); } }
#region //Http请求,httpGet public static iDefine.ChuLiJieGuo httpGet(string url, ref string responseText) { iDefine.ChuLiJieGuo result = new iPublic.iDefine.ChuLiJieGuo(); HttpWebRequest request = null; HttpWebResponse response = null; StreamReader sr = null; try { // 创建一个HTTP请求 request = (System.Net.HttpWebRequest)WebRequest.Create(url); request.Timeout = 30 * 1000; //30秒 request.Method = "GET"; response = (System.Net.HttpWebResponse)request.GetResponse(); //请求状态正确 if (response.StatusCode != HttpStatusCode.OK) { throw new Exception("请求出错"); } sr = new System.IO.StreamReader(response.GetResponseStream(), Encoding.UTF8); //Encoding.GetEncoding("gb2312") responseText = sr.ReadToEnd(); // result.error = 0; } catch (Exception ex) { responseText = ex.Message; result.error = 100; result.message = ex.Message; } finally { if (sr != null) sr.Close(); } return result; } #endregion
#region //ChuLiJieGuo,通用的处理结果返回结构 /// <summary> /// 通用的处理结果返回结构 /// </summary> public class ChuLiJieGuo { public int error = -1; //成功返回0 public string state = ""; //备用的,个人习惯 public string message = ""; //返回的文字信息 //public object[] data = new object[2]; public object data = null, data2 = null, data3 = null, data4 = null, data5 = null; //随意返回点什么 public string callBack = ""; //回调函数 } #endregion