C#的百度地图开发(五)IP定位

前面我们已经实现了坐标定位及前端显示,而坐标的获取一般只在移动设备上,对于PC端难以得到。但是在PC端,我们可以得到相应的IP。在得到了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记录》。

转载请注明出处。

原文地址:https://www.cnblogs.com/sparkleDai/p/7604974.html