Unity获取当前城市天气
来源:互联网 发布:网络直播评论性文章 编辑:程序博客网 时间:2024/05/21 12:20
完善了根据当前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); }}
阅读全文
0 0
- Unity获取当前城市天气
- ajax获取当前城市天气情况
- 基于百度定位获取当前城市请求天气信息
- js获取当前城市,并根据当前城市获得当地天气
- 自动获取城市天气代码
- 城市天气数据获取接口
- 获取指定城市的天气
- 根据城市名获取天气
- webservice示例获取城市天气
- js定位当前城市,获得当前城市的天气
- Jquery学习总结(3)——Jquery获取当前城市的天气信
- iOS获取当前城市
- 获取当前城市
- iOS 获取当前城市
- 获取当前城市
- flex webservice获取城市的天气情况
- android webservice获取城市天气情况
- android webservice获取城市天气情况
- 编程练习二维数组 java
- POJ-3281 Dining Dinic最大流
- codeforces 579D(35/600)
- Android通过HTTP协议实现断点续传下载
- Mongodb 删除重复数据的几个方法
- Unity获取当前城市天气
- Android常用框架----网络请求系列框架
- 项目提示:Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactor
- CSS3转换
- 机器学习笔记六
- java 字典排序
- R语言基础
- HFOI2017.07.10校内赛(普及组)题解
- 跨域资源共享 CORS 详解