您当前的位置: 首页 >  unity

幻世界

暂无认证

  • 3浏览

    0关注

    237博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

【Unity开发小技巧】Unity获取当前所在城市及GPS信息

幻世界 发布时间:2018-04-12 15:13:53 ,浏览量:3

欢迎加入Unity业内qq交流群:956187480

此博客可能不能满足大部分人的需求,仅限于需要获取当前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( www.text);
        Debug.Log(req.content.address_detail.city +" X: "+ req.content.point.x +" Y: "+ req.content.point.x);
    }
    }

而这个接口以网址的形式加载,加载成功后会返回一段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;
    }
}

加载后的数据经过解析后

然后我们就能得到想要的数据了点击打开源码地址链接

json的解析工具,以及源码地址https://download.csdn.net/download/qq_37310110/10343954

欢迎加入Unity业内qq交流群:956187480

关注
打赏
1660704426
查看更多评论
立即登录/注册

微信扫码登录

0.0385s