C#使用百度API通过IP获取地理位置和坐标

百度接口相关说明:http://developer.baidu.com/map/ip-location-api.htm

返回是json格式,首先构建相关反系列化类:

C#使用百度API通过IP获取地理位置和坐标
 1 #region AddressForQueryIPFromBaidu
 2 [Serializable]
 3 public class AddressForQueryIPFromBaidu
 4 {
 5     public string Address { get; set; }
 6     public Content Content { get; set; }
 7     public string Status { get; set; }
 8 }
 9 [Serializable]
10 public class Content
11 {
12     public string Address { get; set; }
13     public Address_Detail Address_Detail { get; set; }
14     public Point Point { get; set; }
15 }
16 [Serializable]
17 public class Address_Detail
18 {
19     public string City { get; set; }
20     public string City_Code { get; set; }
21     public string District { get; set; }
22     public string Province { get; set; }
23     public string Street { get; set; }
24     public string Street_Number { get; set; }
25 }
26 [Serializable]
27 public class Point
28 {
29     public string X { get; set; }
30     public string Y { get; set; }
31 }
32 #endregion
C#使用百度API通过IP获取地理位置和坐标

接口调用方法:

C#使用百度API通过IP获取地理位置和坐标
 1 public static AddressForQueryIPFromBaidu GetAddressFromIP(string ipAddress)
 2 {
 3     string baiduKey = "59722ea6a425fbd81******80ee3ecbb";
 4     string url = "http://api.map.baidu.com/location/ip?ak="+baiduKey+"&ip="+ipAddress+"&coor=bd09ll";
 5     HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
 6     HttpWebResponse response = (HttpWebResponse)request.GetResponse();
 7     System.IO.Stream responseStream = response.GetResponseStream();
 8     System.IO.StreamReader sr = new System.IO.StreamReader(responseStream, System.Text.Encoding.GetEncoding("utf-8"));
 9     string responseText = sr.ReadToEnd();
10     sr.Close();
11     sr.Dispose();
12     responseStream.Close();
13     string jsonData = responseText;
14     JavaScriptSerializer jss = new JavaScriptSerializer();
15     AddressForQueryIPFromBaidu addressForQueryIPFromBaidu = jss.Deserialize<AddressForQueryIPFromBaidu>(jsonData);
16     return addressForQueryIPFromBaidu;
17 }
C#使用百度API通过IP获取地理位置和坐标

 

C#使用百度API通过IP获取地理位置和坐标,布布扣,bubuko.com

C#使用百度API通过IP获取地理位置和坐标

上一篇:WinJs项目介绍


下一篇:Windows 7上安装Microsoft Loopback Adapter(微软环回网卡)