Unity获取当前城市天气

xiaoxiao2021-02-28  128

完善了根据当前IP查询所在城市编码,然后根据城市编码获取当前城市天气,DEMO下载地址

参考http://blog.csdn.net/wjb0108/article/details/41380193
效果:   
代码如下:
using UnityEngine; using System.Collections; using System.IO; using LitJson; using System.Text.RegularExpressions; using System.Xml; using System.Text; public class WeatherTest : MonoBehaviour { private string m_url = "http://www.weather.com.cn/data/cityinfo/";//上海天气 101210101.html public string lbCity;//城市 public string lbTemperature;//温度 public string lbWeahter;//天气 public string lbTime; public string lbHour; public string lbMinute; public string lbColon; private System.DateTime dNow; private string[] dow = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; private float deltaTime = 0.0f; //update Date private float deltaTimeforWeather = 0.0f; // update Weather private float howlongToUpdate = 3600f; void Start() { StartCoroutine(GetExternalIp()); } void Update() { //Date dNow = System.DateTime.Now; lbTime = dNow.ToString("yyyy年MM月dd日 HH:mm:ss ") + dow[System.Convert.ToInt16(dNow.DayOfWeek)]; lbHour = dNow.ToString("HH"); lbMinute = dNow.ToString("mm"); deltaTime += Time.deltaTime; if (deltaTime > 1.0f) { deltaTime = 0.0f; } deltaTimeforWeather += Time.deltaTime; if (deltaTimeforWeather > howlongToUpdate)//每一小时更新一次 { UpdateWeather(); deltaTimeforWeather = 0.0f; } } public void UpdateWeather() { StartCoroutine(GetWeather(CityCodeId)); } //void OnEnable() //{ // UpdateWeather(); //} IEnumerator GetWeather(string CityId)//根据城市ID获取天气信息 { if (m_url != null) { WWW www = new WWW(m_url + CityId + @".html"); Debug.Log("URL:"+m_url + CityId + @".html"); while (!www.isDone) { yield return www; } if (www.text != null) { JsonData jd = JsonMapper.ToObject(www.text); JsonData jdInfo = jd["weatherinfo"]; lbCity = jdInfo["city"].ToString(); lbTemperature = jdInfo["temp1"].ToString() + "~" + jdInfo["temp2"].ToString(); lbWeahter = jdInfo["weather"].ToString(); //。。。。需要的其它信息自己看接口提取 string strImg1 = jdInfo["img1"].ToString(); Debug.Log("city:" + lbCity + "\n weather:" + lbWeahter); } } } string ip; IEnumerator GetExternalIp()//获取当前外网地址 { WWW www = new WWW("http://www.ip138.com/ips138.asp"); while (!www.isDone) { yield return www; } if (www.text != null) { Regex r = new Regex("((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|[1-9])", RegexOptions.None); Match mc = r.Match(www.text); ip = mc.Groups[0].Value; MacIp = ip; StartCoroutine(GetAdrByIp()); } } string MacIp; IEnumerator GetAdrByIp()//根据IP获取当前城市地址 { //string url = "http://www.ip138.com/ips138.asp?ip=" + MacIp; string url = "http://www.ip.cn/index.php?ip=" + MacIp; string regStr = "(?<=<div\\s*id=\\\"result\\\">).*?(?=</div>)"; WWW html = new WWW(url.Trim()); print("IP查询地址:"+url); while (!html.isDone) { yield return html; } if (html.text != null) { Regex reg = new Regex(regStr, RegexOptions.None); Match ma = reg.Match(html.text); City = ma.Value; string resstr; resstr = (ReplaceHtmlTag(City).Trim().Split(':')[2].Substring(0, 6)).ToString(); print(resstr); string[] arr = resstr.Split('省'); Province = arr[0]; City = arr[1].Replace("市", null); Debug.Log("内容:" + City); Xmlfilepath = Application.dataPath + @"/CityWeather/CityCode.xml"; CityCodeId = ReadXml(Xmlfilepath, City, Province); Debug.Log(Province + "省" + City + CityCodeId); UpdateWeather(); } } string Xmlfilepath; string City; string Province; string CityCodeId; public string ReadXml(string filepath, string value, string province) { string strvalue = null; if (File.Exists(filepath)) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(filepath); XmlNodeList nodeList = xmlDoc.SelectSingleNode("China").ChildNodes; foreach (XmlElement xe in nodeList) { if (xe.GetAttribute("name") == province) { foreach (XmlElement x1 in xe.ChildNodes) { foreach (XmlElement x2 in x1.ChildNodes) { if (x2.GetAttribute("name") == value) { strvalue = x2.GetAttribute("weatherCode"); } } } } } } return strvalue; } public static string ReplaceHtmlTag(string html, int length = 0)//移除HTML标签 { string strText = System.Text.RegularExpressions.Regex.Replace(html, "<[^>]+>", ""); strText = System.Text.RegularExpressions.Regex.Replace(strText, "&[^;]+;", ""); if (length > 0 && strText.Length > length) return strText.Substring(0, length); return strText; } void OnGUI() { GUI.Label(new Rect(60, 40, 200, 25), "公 网 IP :" + ip); GUI.Label(new Rect(60, 70, 200, 25), "所在城市 :" + Province + "省" + City + "市"); GUI.Label(new Rect(60, 130, 200, 25), "城市编码:" + CityCodeId); GUI.Label(new Rect(60, 160, 200, 25), "城市天气:" + lbWeahter); GUI.Label(new Rect(60, 190, 200, 25), "城市气温:" + lbTemperature); GUI.Label(new Rect(60, 100, 300, 25), "城市时间:" + lbTime); } }
转载请注明原文地址: https://www.6miu.com/read-37277.html

最新回复(0)