C#调用百度高精度IP定位API通过IP获取地址 |
您所在的位置:网站首页 › 高精度ip定位接口 › C#调用百度高精度IP定位API通过IP获取地址 |
https://api.map.baidu.com/highacciploc/v1
参数:qcip 待定位IP 可选 如果为空则针对定位服务的IP进行定位ak 开发者密钥,即前面申请的东西 extensions 返回结果扩展设定 可选, 0(默认):只返回基础定位结果 1:返回基础定位结果+地址信息 2:返回基础定位结果+周边POI信息 3:返回基础定位结果+地址信息+POI信息 这里我们用1 coord 返回坐标类型 可选, bd09(默认):百度墨卡托坐标 bd09ll:百度经纬度坐标 gcj02:国测局经纬度坐标 完整调用: 在浏览器地址栏输入以下网址 https://api.map.baidu.com/highacciploc/v1?qcip=220.181.38.113&ak=你申请的AK&extensions=1&coord=bd09ll 获取结果如下: 返回值说明: 这里默认用的是返回json格式的数据。 字段说明 content location lat 纬度坐标 基础定位结果extensions=任何值都返回 lng 经度坐标 locid 定位结果唯一ID,用于问题排查 radius 定位结果半径 confidence 定位结果可信度 address_component country 国家 地址信息extensions=1、3返回 province 省份 city 城市 district 区县 street 街道 street_number 门牌号 admin_area_code 行政区划代码(身份证前6位) formatted_address 结构化地址信息 business 商圈信息 pois(1000m以内的最多10条poi) name 名称 周边POI信息extensions=2、3返回 uid POI唯一标识ID address 地址 tag 分类 location lat 纬度 lng 经度 location_description 位置描述信息 result loc_time 定位时间 结果信息extensions=任何值都返回 error 定位结果状态码161:定位成功 167:定位失败 1:服务器内部错误 101:AK参数不存在 200:应用不存在,AK有误请检查重试 201:应用被用户自己禁止 202:应用被管理员删除 203:应用类型错误 210:应用IP校验失败 211:应用SN校验失败 220:应用Refer检验失败 240:应用服务被禁用 251:用户被自己删除 252:用户被管理员删除 260:服务不存在 261:服务被禁用 301:永久配额超限,禁止访问 302:当天配额超限,禁止访问 401:当前并发超限,限制访问 402:当前并发和总并发超限 创建对应的类: 1 [Serializable] //添加序列化特性 2 public class DetailAddress 3 { 4 public DetailContent Content { get; set; } 5 public DetailResult Result { get; set; } 6 } 7 8 [Serializable] 9 public class DetailContent 10 { 11 public DetialLocation Location { get; set; } 12 public string Locid { get; set; } 13 public string Radius { get; set; } 14 public string Confidence { get; set; } 15 public DetailAddress_component Component{get;set;} 16 public string Formatted_address{get;set;} 17 } 18 19 20 [Serializable] 21 public class DetialLocation 22 { 23 public string Lat { get; set; } 24 public string Lng { get; set; } 25 26 } 27 28 [Serializable] 29 public class DetailAddress_component 30 { 31 public string Country { get; set; } 32 public string Province { get; set; } 33 public string City { get; set; } 34 public string Distinct { get; set; } 35 public string Street { get; set; } 36 public string Street_Number { get; set; } 37 public string Admin_Area_Code { get; set; } 38 39 } 40 41 [Serializable] 42 public class DetailResult 43 { 44 public string Error { get; set; } 45 public string Loc_time { get; set; } 46 }获取数据: public static DetailAddress GetDetailAddressByBaiduAPI(string IPAddress) { System.Net.HttpWebRequest request; System.Net.HttpWebResponse response; string url = string.Format("https://api.map.baidu.com/highacciploc/v1?qcip={0}&qterm=pc&extensions=1&ak=你的AK&coord=bd09ll", IPAddress); try { request = HttpWebRequest.Create(url) as System.Net.HttpWebRequest; response = request.GetResponse() as System.Net.HttpWebResponse; using(System.IO.Stream stream = response.GetResponseStream()) { using(System.IO.StreamReader sr = new System.IO.StreamReader(stream, System.Text.Encoding.UTF8)) { string Data = sr.ReadToEnd(); System.Web.Script.Serialization.JavaScriptSerializer serializer = new JavaScriptSerializer(); DetailAddress detail = serializer.Deserialize(Data); return detail; } } } catch (Exception) { return null; } }查看结果: //使用 DetailAddress detail = GetDetailAddressByBaiduAPI("114.114.114.114"); //查看街道 System.Console.WriteLine(detail.Content.Location.Street);
============================================================== 普通IP定位API的使用方法API首页:http://lbsyun.baidu.com/index.php?title=webapi/ip-api 使用方法跟前面一样,也是先申请密钥AK,然后拼写发送HTTP/HTTPS请求的URL 接口参数: ip 指定IP地址,如果不指定,则获取当前IP地址的位置信息ak 开发者密钥sn 可选,若用户所用AK的校验方式为SN校验时该参数必须coor 输出的坐标格式(WGS84:为一种大地坐标系,也是目前广泛使用的GPS全球卫星定位系统使用的坐标系;GCJ02:表示经过国测局加密的坐标;BD09:为百度坐标系,其中bd09ll表示百度经纬度坐标,bd09mc表示百度墨卡托米制坐标;) 请求网址: http://api.map.baidu.com/location/ip https://api.map.baidu.com/location/ip 使用方式:http://api.map.baidu.com/location/ip?ak=请输入您的AK&coor=bd09ll 或 https://api.map.baidu.com/location/ip?ak=请输入您的AK&coor=bd09ll
其实WebAPI的调用方式都一样, 一般返回的都 是JSON或JSONP格式的数据,C# 都 可以调用,只要创建对应的序列化的类就可以了。 对于获取到的JSON数据,如果觉得比较乱,可以在线解析一下,创建类的时候就会方便很多了。 下图是调用API是获取到的结果 ![]() |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |