此博客可能不能满足大部分人的需求,仅限于需要获取当前ip所属城市,及城市的经纬度信息。
实现过程并不难。主要是依据于百度的一个api接口
登录后复制
string url = "http://api.map.baidu.com/location/ip?ak=bretF4dm6W5gqjQAXuvP0NXW6FeesRXb&coor=bd09ll"; void Start() { StartCoroutine(Request()); } IEnumerator Request() { WWW www = new WWW(url); yield return www; if (string.IsNullOrEmpty(www.error)) { Debug.Log(www.text); ResponseBody req = JsonConvert.DeserializeObject<ResponseBody>( www.text); Debug.Log(req.content.address_detail.city +" X: "+ req.content.point.x +" Y: "+ req.content.point.x); } }1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.
而这个接口以网址的形式加载,加载成功后会返回一段json数据
这时候就需要用到json的解析工具了,解析为固定的模式。而这个模式好像是百度api接口已经定好的,这里我们用Newtonsoft.Json。
类的固定格式如下
登录后复制
public class ResponseBody{ public string address; public Content content; public int status;}public class Content{ public string address; public Address_Detail address_detail; public Point point;}public class Address_Detail{ public string city; public int city_code; public string district; public string province; public string street; public string street_number; public Address_Detail(string city, int city_code, string district, string province, string street, string street_number) { this.city = city; this.city_code = city_code; this.district = district; this.province = province; this.street = street; this.street_number = street_number; }}public class Point{ public string x; public string y; public Point(string x, string y) { this.x = x; this.y = y; }}1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.42.43.
加载后的数据经过解析后
然后我们就能得到想要的数据了点击打开源码地址链接
免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删