我想实现天气信息,它将向我显示取决于我的经度和纬度的结果。
我的应用程序正在从 GPS 获取坐标,因此获取它们不是问题。唯一的事情是,我想显示离我最近的城市的一些天气信息,并且它有天气信息。
你能给我一些想法和解决方案。
您如何看待谷歌天气 API?
如何使它搜索最近的以及如何赋予此代码经纬度。
public static void GoogleWeather(string location)
{
HttpWebRequest GoogleRequest;
HttpWebResponse GoogleResponse = null;
XmlDocument GoogleXMLdoc = null;
try
{
GoogleRequest = (HttpWebRequest)WebRequest.Create("http://www.google.com/ig/api?weather=" + string.Format(location));
GoogleResponse = (HttpWebResponse)GoogleRequest.GetResponse();
GoogleXMLdoc = new XmlDocument();
GoogleXMLdoc.Load(GoogleResponse.GetResponseStream());
XmlNode root = GoogleXMLdoc.DocumentElement;
XmlNodeList nodeList1 = root.SelectNodes("weather/forecast_information");
HttpContext.Current.Response.Write("<b>City : " + nodeList1.Item(0).SelectSingleNode("city").Attributes["data"].InnerText + "</b>");
XmlNodeList nodeList = root.SelectNodes("weather/current_conditions");
HttpContext.Current.Response.Write("
");
HttpContext.Current.Response.Write("
<table class="bordered" cellpadding="5">
<tbody><tr><td><b><big><nobr>" + nodeList.Item(0).SelectSingleNode("temp_c").Attributes["data"].InnerText + " °C | " + nodeList.Item(0).SelectSingleNode("temp_f").Attributes["data"].InnerText + " °F</nobr></big></b>");
HttpContext.Current.Response.Write("<b>Current:</b> " + nodeList.Item(0).SelectSingleNode("condition").Attributes["data"].InnerText + "");
HttpContext.Current.Response.Write("" + nodeList.Item(0).SelectSingleNode("wind_condition").Attributes["data"].InnerText + "");
HttpContext.Current.Response.Write(nodeList.Item(0).SelectSingleNode("humidity").Attributes["data"].InnerText);
nodeList = root.SelectNodes("descendant::weather/forecast_conditions");
foreach (XmlNode nod in nodeList)
{
HttpContext.Current.Response.Write("</td>
<td align="center">" + nod.SelectSingleNode("day_of_week").Attributes["data"].InnerText+ "");
HttpContext.Current.Response.Write("<img src="http://www.google.com" + nod.SelectSingleNode("icon").Attributes["data"].InnerText + "" alt="" + nod.SelectSingleNode("condition").Attributes["data"].InnerText + "">");
HttpContext.Current.Response.Write(nod.SelectSingleNode("low").Attributes["data"].InnerText + "°F | ");
HttpContext.Current.Response.Write(nod.SelectSingleNode("high").Attributes["data"].InnerText + "°F");
}
HttpContext.Current.Response.Write("</td>
</tr>
</tbody></table>
");
}
catch (System.Exception ex)
{
HttpContext.Current.Response.Write(ex.Message);
}
finally
{
GoogleResponse.Close();
}
}