C#使用百度API通过IP获取地理位置和坐标
2014-04-22 15:28
1001 查看
百度接口相关说明:http://developer.baidu.com/map/ip-location-api.htm
返回是json格式,首先构建相关反系列化类:
接口调用方法:
返回是json格式,首先构建相关反系列化类:
#region AddressForQueryIPFromBaidu [Serializable] public class AddressForQueryIPFromBaidu { public string Address { get; set; } public Content Content { get; set; } public string Status { get; set; } } [Serializable] public class Content { public string Address { get; set; } public Address_Detail Address_Detail { get; set; } public Point Point { get; set; } } [Serializable] public class Address_Detail { public string City { get; set; } public string City_Code { get; set; } public string District { get; set; } public string Province { get; set; } public string Street { get; set; } public string Street_Number { get; set; } } [Serializable] public class Point { public string X { get; set; } public string Y { get; set; } } #endregion
接口调用方法:
public static AddressForQueryIPFromBaidu GetAddressFromIP(string ipAddress) { string baiduKey = "59722ea6a425fbd81******80ee3ecbb"; string url = "http://api.map.baidu.com/location/ip?ak="+baiduKey+"&ip="+ipAddress+"&coor=bd09ll"; HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); System.IO.Stream responseStream = response.GetResponseStream(); System.IO.StreamReader sr = new System.IO.StreamReader(responseStream, System.Text.Encoding.GetEncoding("utf-8")); string responseText = sr.ReadToEnd(); sr.Close(); sr.Dispose(); responseStream.Close(); string jsonData = responseText; JavaScriptSerializer jss = new JavaScriptSerializer(); AddressForQueryIPFromBaidu addressForQueryIPFromBaidu = jss.Deserialize<AddressForQueryIPFromBaidu>(jsonData); return addressForQueryIPFromBaidu; }
相关文章推荐
- 通过百度普通IP定位API获取IP的地理位置
- C#通过IP获取用户地理位置省份城市的接口使用
- C#实现微信结合百度api获取当前用户地理位置的方法
- java根据ip地质调用百度定位api服务获取地理位置示例
- 使用百度api根据ip获取位置
- 百度开发者API接口:获取经纬度和详细地理位置(Geocoding API和IP定位API)
- 续【C#微信结合百度api获取当前用户的地理位置】代码详解
- java根据ip地质调用百度定位api服务获取地理位置示例
- C#微信结合百度api获取当前用户的地理位置
- 利用google提供的API(JavaScript接口)获取网站访问者IP地理位置的代码详解
- 通过GeoIP2分析访问者IP获取地理位置信息
- Revit中如何通过API获取对象的位置坐标和旋转角度
- 使用html5定位经纬度然后用百度api获取城市等位置信息
- 通过ip获取地理位置信息
- java web 通过ip获取当前地理位置
- java调用百度定位api服务获取地理位置示例
- C# 网络编程之通过ip地址获取地理位置(补充)
- 通过GeoIP2分析访问者IP获取地理位置信息
- ahjesus可供C#使用的ip地理位置查询插件