Unity之根据ip获取城市和天气信息

采用心知天气api实现
ps:这个需求的实现方式蛮多,但是通过百度api的大多已失效,本方案2022-08-17已测试通过
pps:不要翻墙,不然获取不到城市信息!!!CityId没有就得不到最终的天气信息了

/*
     * 心知天气官网:https://www.seniverse.com/
     * 通过ip直接获取城市信息:https://api.live.bilibili.com/client/v1/Ip/getInfoNew (备用)
     */

    // ipv6:http://icanhazip.com
    // ipv4:https://api.ipify.org  (推荐用ipv4,ipv6返回的results里面的[]可能为空)
    private const string publicIpQueryWebsite = @"https://api.ipify.org";
    private const string privateKey = "去官网购买免费版把私钥填写在这里~";

    [SerializeField] private Text cityNameText, cityTemperatureText;

    public void Awake()
    {
        StartCoroutine(GetWeatherInfos());
    }

    IEnumerator GetWeatherInfos()
    {
        UnityWebRequest publicIpReq = UnityWebRequest.Get(publicIpQueryWebsite);
        yield return publicIpReq.SendWebRequest();
        if (!string.IsNullOrEmpty(publicIpReq.error))
        {
            Debug.Log($"查询公网ip报错:{publicIpReq.error}");
            yield break;
        }

        string cityUri = "https://api.seniverse.com/v3/location/search.json?key=" + privateKey + "&q=" + publicIpReq.downloadHandler.text;
        UnityWebRequest cityReq = UnityWebRequest.Get(cityUri);
        yield return cityReq.SendWebRequest();
        if (!string.IsNullOrEmpty(cityReq.error))
        {
            Debug.Log($"根据公网ip得到城市信息报错:{publicIpReq.error}");
            yield break;
        }

        // 城市信息范例:
        // {
        //  "results": [{
        //   "id": "WT3Q0FW9ZJ3Q",
        //   "name": "武汉",
        //   "country": "CN",
        //   "path": "武汉,武汉,湖北,中国",
        //   "timezone": "Asia/Shanghai",
        //   "timezone_offset": "+08:00"
        //  }]
        // }
        JSONNode cityDataNode = JSON.Parse(cityReq.downloadHandler.text);
        string cityId = cityDataNode["results"][0]["id"];

        string weatherUri = "https://api.seniverse.com/v3/weather/now.json?key=" + privateKey + "&location=" + cityId + "&language=zh-Hans&unit=c";
        UnityWebRequest weatherReq = UnityWebRequest.Get(weatherUri);
        yield return weatherReq.SendWebRequest();

        if (!string.IsNullOrEmpty(weatherReq.error))
        {
            Debug.Log($"获取城市天气信息报错:{weatherReq.error}");
            yield break;
        }

        // 天气信息范例:
        // {
        //  "results": [{
        //   "location": {
        //    "id": "WT3Q0FW9ZJ3Q",
        //    "name": "武汉",
        //    "country": "CN",
        //    "path": "武汉,武汉,湖北,中国",
        //    "timezone": "Asia/Shanghai",
        //    "timezone_offset": "+08:00"
        //   },
        //   "now": {
        //    "text": "晴",
        //    "code": "0",
        //    "temperature": "35"
        //   },
        //   "last_update": "2022-08-17T11:20:04+08:00"
        //  }]
        // }
        JSONNode weatherDataNode = JSON.Parse(weatherReq.downloadHandler.text);
        cityNameText.text = weatherDataNode["results"][0]["location"]["name"];
        cityTemperatureText.text = weatherDataNode["results"][0]["now"]["temperature"] + "°";
    }
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值