C#百度定位API使用
来源:互联网 发布:古墓丽影崛起配置优化 编辑:程序博客网 时间:2024/05/30 04:46
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<baidu_location.AddressForQueryIPFromBaidu>(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-api2.在服务接口文档目录下有一个请求参数表格,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;
中间如有缺少类库引用可以自己添加或参考我之前的博客,也可留言。
本文代码或有不足,中间可根据自己需求进行代码更改。
阅读全文
0 0
- C#百度定位API使用
- c#百度IP定位API使用方法
- android百度地图---使用API定位不成功
- 使用百度地图api实现定位功能。
- HTML5手机定位-使用百度API
- 百度地图定位API,locationClient的使用
- android 百度定位API使用教程
- Android百度定位API的使用
- C#使用百度翻译API
- 百度定位API使用方法
- 百度定位API使用方法
- 百度定位API使用方法
- 【经验小谈】百度定位api使用 百度定位demo 百度地图快速定位 百度地图获取当前位置 获取自己的位置 百度定位示例
- 手机自动定位 百度地图API使用的文章
- 手机自动定位 百度地图API使用的文章
- iOS百度地图API的使用---定位功能
- 使用百度API实现热点(WIFI)、GPS、基站定位
- 使用百度地图API实现定位的效果。
- scala初学之函数定义、流程控制、异常处理入门
- 重回博客林汉三
- 迭代方法替代for循环
- C++中的虚函数(表)实现机制以及用C语言对其进行的模拟实现
- 能提高生产率的Markdown介绍
- C#百度定位API使用
- leetcode 437. Path Sum III 深度优先遍历DFS
- Win7如何搭建React-Native 环境(64位系统)
- java前景怎么样?就业领域有哪些?
- Capstone训练营3
- angular.js路由页面缓存问题
- LVM扩容
- Android Studio新功能解析,Instant Run
- StringBoot多数据源支持