备份获取位置信息代码

本文介绍了一种使用百度地图API来解析IP地址并获取详细地理位置信息的方法。通过调用特定的API接口,并提供必要的参数如API密钥和IP地址,可以获取到包括经纬度坐标在内的精确位置数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  [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; }
    }

    public class WebLocation
    {
        public static AddressForQueryIPFromBaidu GetAddressFromIP(string ipAddress)
        {
          var  ip = Net.GetIP();
            string baiduKey = "CkpxXeOIsBMtgI48ui1BLo7gvmtR5GIa";
            string urllocation = "http://api.map.baidu.com/location/ip?ak=" + baiduKey + "&ip=" + ip + "&coor=bd09ll";

            //string urllocation = "http://api.map.baidu.com/geocoder/v2/?location=36.68278473,117.02496707&output=xml&pois=1&ak=" + baiduKey;
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(urllocation);
            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;
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值