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"] + "°";
    }

猜你喜欢

转载自blog.csdn.net/itsxwz/article/details/126387392