做一个输入城市名称,就可以得出该城市天气的功能,也可获取未来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