根据当前公网IP获取当前城市天气

根据不固定公网IP获取当前城市实时天气

获取本机公网IP
根据公网IP查找城市
通过获取城市解析天气

 IEnumerator GetIP()
    {
        UnityWebRequest wwwWebIp = UnityWebRequest.Get(@"http://icanhazip.com/");
        yield return wwwWebIp.SendWebRequest();
        if (wwwWebIp.isNetworkError || wwwWebIp.isHttpError)
        {
            yield break;
        }
        else
        {
            //  Debug.Log(wwwWebIp.downloadHandler.text);

        }
        string urlWeather = "https://api.seniverse.com/v3/location/search.json?key=SF0kp6pydMUKGZcEr&q=" + wwwWebIp.downloadHandler.text;//根据IP找城市

        UnityWebRequest wwwQueryCity = UnityWebRequest.Get(urlWeather);
        yield return wwwQueryCity.SendWebRequest();
        if (wwwQueryCity.isNetworkError)
        {
            yield break;
        }
        else
        {
            //  Debug.Log(wwwQueryCity.downloadHandler.text);
        }
        JObject cityData = JsonConvert.DeserializeObject<JObject>(wwwQueryCity.downloadHandler.text);
        string cityId = cityData["results"][0]["id"].ToString();


        city = cityData["results"][0]["name"].ToString();

        string[] arry = cityData["results"][0]["path"].ToString().Split(',');
        province = arry[2];

        string urlWeathers = "https://api.seniverse.com/v3/weather/now.json?key=SF0kp6pydMUKGZcEr&location=" + cityId + "&language=zh-Hans&unit=c";//根据城市找天气

        UnityWebRequest wwwWeather = UnityWebRequest.Get(urlWeathers);
        yield return wwwWeather.SendWebRequest();

        if (wwwWeather.isNetworkError)
        {
            yield break;
        }
        else
        {
             Debug.Log(wwwWeather.downloadHandler.text);
        }

        //解析天气
        try
        {
            JObject weatherData = JsonConvert.DeserializeObject<JObject>(wwwWeather.downloadHandler.text);
            string spriteName = string.Format("Weather/{0}@2x", weatherData["results"][0]["now"]["code"].ToString());

            //天气文字
            //Debug.Log(weatherData["results"][0]["now"]["text"].ToString()); //天气
            //Debug.Log(weatherData["results"][0]["now"]["temperature"].ToString()); //温度

         

        }
        catch (System.Exception ex)
        {
            Debug.Log(ex.Message);
        }


 
    }

#注意事项
string urlWeather = “https://api.seniverse.com/v3/location/search.json?key=SF0kp6pydMUKGZcEr&q=” + wwwWebIp.downloadHandler.text;
当前key值需要注册https://www.seniverse.com/ 控制台——产品管理——添加产品——获取公钥或者私钥 当前免费版本

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值