C#調用百度高精度IP定位API經過IP獲取地址

此API如今已經被關閉,不能再正常使用。javascript

在文末加入了普通IP定位API的使用方法php

==============================================================java

API首頁:http://lbsyun.baidu.com/index.php?title=webapi/high-acc-ipweb

一、申請百度帳號,建立應用,獲取密鑰(AK)json

http://lbsyun.baidu.com/apiconsole/keyapi

啓用服務:,能夠根據本身需求來鉤選,這裏先全選了。瀏覽器

請求校驗方式:IP白名單校驗/SN檢驗,這裏選擇IP白名單校驗。即在下面填上你訪問機器的IP地址。如(114.114.114.114)。服務器

單擊提交,得到AK併發

二、獲取方式:ide

服務地址:

 http://api.map.baidu.com/highacciploc/v1
 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=一、3返回
province 省份
city 城市
district 區縣
street 街道
street_number 門牌號
admin_area_code 行政區劃代碼(身份證前6位)
formatted_address 結構化地址信息
business 商圈信息
pois(1000m之內的最多10條poi) name 名稱 周邊POI信息extensions=二、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<DetailAddress>(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是獲取到的結果

 
 

 而後咱們在線解析 一下,解析的網址爲

http://www.json.cn/

 

解析後的結果以下

而後咱們建立對應的類,就能夠在C# 中進行調用了。

若是獲取地址不成功,能夠對照上面的錯誤碼查找緣由。

相關文章
相關標籤/搜索