Unity获取各地天气

做一个输入城市名称,就可以得出该城市天气的功能,也可获取未来15天的天气。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class positionTool
{
    #region ip对应城市
    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()
        {

        }

        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()
        {

        }

        public Point(string x, string y)
        {
            this.x = x;
            this.y = y;
        }
    }
    #endregion

}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestIpPosition : MonoBehaviour
{
    /// <summary>
    /// 获取位置信息
    /// </summary>
    string Posurl = "http://api.map.baidu.com/location/ip?ak=bretF4dm6W5gqjQAXuvP0NXW6FeesRXb&coor=bd09ll";

    /// <summary>
    /// 获取天气
    /// </summary>
    //string Weatherurl = "http://t.weather.sojson.com/api/weather/city/";
    string Weatherurl = "http://t.weather.sojson.com/api/weather/city/";

    void Start()
    {
        //获取位置
        StartCoroutine(RequestPos());       
    }


    IEnumerator RequestPos()
    {
        WWW www = new WWW(Posurl);
        yield return www;

        if (string.IsNullOrEmpty(www.error))
        {
            positionTool.ResponseBody t = LitJson.JsonMapper.ToObject<positionTool.ResponseBody>(www.text);
            Debug.Log(t.content.address_detail.city);
            //获取天气
            //StartCoroutine(RequestWeather(weatherTool.GetWeatherId(t.content.address_detail.city)));
            StartCoroutine(RequestWeather(weatherTool.GetWeatherId("北京市")));
        }
    }

    IEnumerator RequestWeather(int id)
    {
        WWW www = new WWW(Weatherurl + id.ToString());
        Debug.Log(Weatherurl + id.ToString());
        yield return www;

        if (string.IsNullOrEmpty(www.error))
        {
            Debug.Log(www.text);
            weatherTool.WeathBody t = LitJson.JsonMapper.ToObject<weatherTool.WeathBody>(www.text);
            Debug.Log(t.data.forecast[0].type);

        }
    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class weatherTool
{
    public static Dictionary<string, int> PosToId = new Dictionary<string, int>();

    public static bool initDic = false;

    public static int GetWeatherId(string name)
    {
        int id = 0;
        if(!initDic)
        {
            initDic = true;
            TextAsset ta = Resources.Load<TextAsset>("WeatherId");
            List<Pos2Id> temp = LitJson.JsonMapper.ToObject<List<Pos2Id>>(ta.text);
            foreach(Pos2Id t in temp)
            {
                PosToId[t.placeName] = t.id;
            }
        }
        for(int i=1;i<name.Length;i++)
        {
            string tn = name.Substring(0, i);
            if(PosToId.ContainsKey(tn))
            {
                id = PosToId[tn];
            }
        }
        return id;
    }

    public class Pos2Id
    {
        public string placeName;
        public int id;

        public Pos2Id()
        {

        }
        public Pos2Id(string name,int id)
        {
            placeName = name;
            this.id = id;
        }
    }

    public class WeathBody
    {
        public string time;
        public CityInfo cityInfo;
        public string date;
        public string message;
        public int status;
        public WeathData data;
    }

    public class CityInfo
    {
        public string city;
        public string cityId;
        public string parent;
        public string updateTime;
    }

    public class WeathData
    {
        public string shidu;
        public double pm25;
        public double pm10;
        public string quality;
        public string wendu;
        public string ganmao;
        public WeathDetailData yesterday;
        public WeathDetailData[] forecast;
    }

    public class WeathDetailData
    {
        public string date;
        public string sunrise;
        public string high;
        public string low;
        public string sunset;
        public double aqi;
        public string ymd;
        public string week;
        public string fx;
        public string fl;
        public string type;
        public string notice;
    }

}

欢迎讨论Q 1390659165

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小姑娘不爱写代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值