C#百度定位API使用

1、定义一个存储百度API返回json结果对象的类:

注:解析json字符串的方式有很多,无需局限于本文这一种。

    public class baidu_location
    {
        [Serializable]
        //存储百度定位API返回结果类
        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; }
        }
    }

2、定位方法函数:

        public static baidu_location.AddressForQueryIPFromBaidu GetAddressFromIP()
        {
            string baiduKey = "";//值为自己申请的ak值,填进去即可
            string url = "http://api.map.baidu.com/location/ip?ak=" + baiduKey + "&coor=bd09ll";
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.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;
                
                baidu_location.AddressForQueryIPFromBaidu addressForQueryIPFromBaidu = JsonConvert.DeserializeObject(jsonData);
                if(Convert.ToInt32(addressForQueryIPFromBaidu.Status)==0)
                    return addressForQueryIPFromBaidu;
                else
                {
                    //根据需求,参考官方文档可以添加更多判断项
                    MessageBox.Show("定位失败");
                    return null;
                    
                }
            }
            catch (Exception e) {
                //MessageBox.Show("error occured when get location from ip:"+e.ToString());
                MessageBox.Show("定位失败,出现异常");
                return null;
            }
        }

3、ak申请方法:

1.百度地图API官方文档地址:

http://lbsyun.baidu.com/index.php?title=webapi/ip-api
2.在服务接口文档目录下有一个请求参数表格,ak参数含义选项下有API控制台可以点击,点击进去即可申请,里面介绍比较清楚,这里不再赘述。

3.如无特殊要求,选择服务端和ip白名单(0.0.0.0/0)即可

4.申请完之后将ak填到定位函数里

4、调用实例:

            baidu_location.AddressForQueryIPFromBaidu location_msg = Method.GetAddressFromIP();
            if (location_msg == null)
                msg_content.location = "";
            else
                msg_content.location = location_msg.Content.Address_Detail.Province + location_msg.Content.Address_Detail.City;

中间如有缺少类库引用可以自己添加或参考我之前的博客,也可留言。

本文代码或有不足,中间可根据自己需求进行代码更改。


你可能感兴趣的:(C#)