C#的百度地图开发(五)IP定位
2015-01-14 11:10
465 查看
前面我们已经实现了坐标定位及前端显示,而坐标的获取一般只在移动设备上,对于PC端难以得到。但是在PC端,我们可以得到相应的IP。在得到了IP之后,我们就可以进行定们了。代码如下:
(1).使用const定义Api的URL模板。
(2).百度KEY自行申请后填入。
(3).IpLOcationResult接收json数据的反序列化结果,该类的实现如下
测试代码如下:
测试结果如下图;
注:
(1).可以看到依据IP已经得到了定位的结果。不过IP定位的结果精度只能到城市,所以是一个精略的定位结果,如果需要精确定位,还是要使用经纬度坐标。
(2).从结果中可以看到该IP对应的经纬度,所以可以通过这个经纬度在前端的地图上显示定位(具体的可以参照前面的文章)。
那对于网站,要如何去得到这些IP的值呢?请参看后文《C#的百度地图开发(六)用户访问网页的IP记录》。
转载请注明出处。
public class BaiduMap { /// <summary> /// 依据IP获取定位信息的URL模板。 /// 参数1:百度地图API的KEY。 /// 参数2:IP。 public const string IP_LOCATION_URL_TEMPLATE = "http://api.map.baidu.com/location/ip?ak={0}&ip={1}&coor=bd09ll"; /// <summary> /// 依据IP获取定位信息 /// </summary> /// <param name="coordinate">坐标</param> /// <returns></returns> public static IpLocationResult FetchLocation(String ip) { if (String.IsNullOrWhiteSpace(ip)) { return null; } String ipLocationUrl = String.Format(IP_LOCATION_URL_TEMPLATE, MAP_KEY_BAI_DU, ip); String responseText = RequestHelper.RequestUrl(ipLocationUrl, null); if (String.IsNullOrWhiteSpace(responseText)) { return null; } IpLocationResult locationResult = null; try { locationResult = Newtonsoft.Json.JsonConvert.DeserializeObject<IpLocationResult>(responseText); } catch (Exception) { return null; } return locationResult; } #endregion }注;
(1).使用const定义Api的URL模板。
(2).百度KEY自行申请后填入。
(3).IpLOcationResult接收json数据的反序列化结果,该类的实现如下
namespace MapApi.Baidu { [Serializable] public class IpLocationResult { /// <summary> /// 状态 /// </summary> public String status { get; set; } /// <summary> /// 地址 /// </summary> public String address { get; set; } /// <summary> /// 内容 /// </summary> public IpLocationResult_Content content { get; set; } } #region IpLocationResult_Content /// <summary> /// 定位结果文本 /// </summary> [Serializable] public class IpLocationResult_Content { /// <summary> /// 地址 /// </summary> public String address { get; set; } /// <summary> /// 地址明细 /// </summary> public IpLocationResult_Content_AddressDetail address_detail { get; set; } /// <summary> /// 经纬度 /// </summary> public Coordinate point { get; set; } } /// <summary> /// 定位结果文本之地址明细 /// </summary> [Serializable] public class IpLocationResult_Content_AddressDetail { /// <summary> /// 城市 /// </summary> public String city { get; set; } /// <summary> /// 城市代码 /// </summary> public String city_code { get; set; } /// <summary> /// 地区 /// </summary> public String district { get; set; } /// <summary> /// 省份 /// </summary> public String province { get; set; } /// <summary> /// 街道 /// </summary> public String street { get; set; } /// <summary> /// 门牌号 /// </summary> public String street_number { get; set; } } #endregion }
测试代码如下:
protected void btnTest_Click(object sender, EventArgs e) { String ip = "47.153.128.1"; IpLocationResult ipLocationResult = BaiduMap.FetchLocation(ip); Alert.Show(ipLocationResult.status.ToString()); }
测试结果如下图;
注:
(1).可以看到依据IP已经得到了定位的结果。不过IP定位的结果精度只能到城市,所以是一个精略的定位结果,如果需要精确定位,还是要使用经纬度坐标。
(2).从结果中可以看到该IP对应的经纬度,所以可以通过这个经纬度在前端的地图上显示定位(具体的可以参照前面的文章)。
那对于网站,要如何去得到这些IP的值呢?请参看后文《C#的百度地图开发(六)用户访问网页的IP记录》。
转载请注明出处。
相关文章推荐
- C#的百度地图开发 IP定位
- C#的百度地图开发(四)前端显示与定位
- C#的百度地图开发 前端显示与定位
- C#的百度地图开发(四)前端显示与定位
- C#的百度地图开发(四)前端显示与定位
- Android版本百度地图开发(四)——定位
- IP定位 C#
- 百度地图Android SDK 2.0开发指南2-结合百度定位SDK在百度地图上显示当前位置
- 百度地图开发--实现打开地图自动定位到上次定位点然后在定位到自己的位置
- [转]Visual C#.Net网络程序开发之TCP/IP
- Android 百度地图定位(手动+自动) 安卓开发教程
- 百度地图学习笔记---ip定位
- C#网络编程(一)——C#开发TCP/IP聊天室
- C#TCP/IP网络应用开发包SocketWrench .NET详细介绍
- android百度地图开发之自动定位所在位置与固定位置进行驾车,步行,公交路线搜索
- Arcgis andoid开发之应用百度地图接口实现精准定位与显示
- Visual C#.Net网络程序开发之TCP/IP
- 用.NET(C#)实现IP定位地理位置
- [转]Visual C#.Net网络程序开发之TCP/IP
- Android 百度地图开发(二)--- 定位功能之MyLocationOverlay,PopupOverlay的使用