如何在 C# 中使用天气 api [关闭]

2024-01-15

我想实现天气信息,它将根据我的经度和纬度向我显示结果。

我的应用程序从 GPS 获取坐标,因此获取它们不是问题。唯一的问题是,我想显示离我最近的城市的一些天气信息,并且它有天气信息。

你能给我一些想法和解决方案吗?

您对 Google 天气 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();  
        }  
    }  

其实不久前我也遇到过类似的问题。

我决定选择 Wunderground,因为我在使用 Google 天气时遇到了太多问题。 *编辑,我在 wunderground 下方添加了一个可用的谷歌示例。谷歌 api 似乎需要城市名称或邮政编码。

这是我的例子,请原谅我的马虎代码,凭记忆输入。

public static DayOfWeek Wunderground(string latlong)
    {
        //Insert your API key in the below URL
        //string latlong = "37.8,-122.4";
        string url = "http://api.wunderground.com/api/*insertyourapikeyhere*/geolookup/conditions/forecast/q/"+latlong+".xml";

        HttpWebRequest web = (HttpWebRequest)WebRequest.Create(url);
        web.UseDefaultCredentials = true;
        web.PreAuthenticate = true;
        web.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.168 Safari/535.19";
        web.GetResponse();
        //Get Dopplar Image
        var dop = "http://api.wunderground.com/api/7dca468f5dd2ff1b/animatedradar/q/TX/Houston.gif?newmaps=1&timelabel=1&width=640&height=480&timelabel.y=10&num=15&delay=50";
        WebClient wc = new WebClient();
        wc.DownloadFile(dop, "dop.gif");

        //Prepare my custom property
        var d = new WeatherLib.DayOfWeek();
        d.Current = new WDay();
        WDay forecast = new WDay();
        var conditions = d;
        var xmlConditions = new XmlDocument();

        //download the api xml
        XDocument api = XDocument.Load(url);
        api.Save("api.xml");
        xmlConditions.Load(string.Format(@"api.xml"));

        XmlNodeList list = xmlConditions.SelectNodes("/response/current_observation");
        WDay current = d.Current;

        //Parse out the XML data
        foreach (XmlNode node in list)
        {
            current.WeatherText = node.SelectSingleNode("weather").InnerText;
            current.TempCurrentF = node.SelectSingleNode("temp_f").InnerText;
            current.TempCurrentC = node.SelectSingleNode("temp_c").InnerText;
            current.Humidity = node.SelectSingleNode("relative_humidity").InnerText;
            current.WindDirection = node.SelectSingleNode("wind_dir").InnerText;
            current.WindSpeedM = node.SelectSingleNode("wind_mph").InnerText;
            current.WindSpeedK = node.SelectSingleNode("wind_kph").InnerText;
            current.Barometer = node.SelectSingleNode("pressure_mb").InnerText;
            current.HeatIndexF = node.SelectSingleNode("heat_index_f").InnerText;
            current.HeatIndexC = node.SelectSingleNode("heat_index_c").InnerText;
            current.WindChill = node.SelectSingleNode("windchill_string").InnerText;
            current.UVIndex = node.SelectSingleNode("UV").InnerText;
            current.RainAmount = node.SelectSingleNode("precip_today_in").InnerText;
            current.Visibility = node.SelectSingleNode("visibility_mi").InnerText;
            current.DewPoint = node.SelectSingleNode("dewpoint_f").InnerText;


        }return d;
    }

谷歌示例

public static DayOfWeek Google(string zip)
    {
        string urlstring = "http://www.google.com/ig/api?weather="+zip;
        var d = new WeatherLib.DayOfWeek();
        d.Monday = new WDay();
        d.Tuesday = new WDay();
        d.Wednesday = new WDay();
        d.Thursday = new WDay();
        d.Friday = new WDay();
        d.Saturday = new WDay();
        d.Sunday = new WDay();
        d.Current = new WDay();

        WDay forecast = new WDay();
        var conditions = d;
        var xmlConditions = new XmlDocument();
        XDocument api = XDocument.Load(urlstring);
        api.Save("api.xml");
        xmlConditions.Load(string.Format(@"api.xml"));
        if (xmlConditions.SelectSingleNode("xml_api_reply/weather/problem_cause") != null)
        {
            conditions = null;
        }
        else
        {

            var singleNode = xmlConditions.SelectSingleNode("xml_api_reply/weather/current_conditions/condition");
            if (singleNode != null)
                if (singleNode.Attributes != null)
                    conditions.Current.WeatherText =
                        singleNode.Attributes[
                            "data"].InnerText;
            var node = xmlConditions.SelectSingleNode("xml_api_reply/weather/current_conditions/temp_c");
            if (node != null)
                if (node.Attributes != null)
                    conditions.Current.TempCurrentC =
                        node.Attributes["data"]
                            .InnerText;
            var selectSingleNode1 = xmlConditions.SelectSingleNode("xml_api_reply/weather/current_conditions/temp_f");
            if (selectSingleNode1 != null)
                if (selectSingleNode1.Attributes != null)
                    conditions.Current.TempCurrentF =
                        selectSingleNode1.Attributes["data"]
                            .InnerText;
            var singleNode1 = xmlConditions.SelectSingleNode("xml_api_reply/weather/current_conditions/humidity");
            if (singleNode1 != null)
                if (singleNode1.Attributes != null)
                    conditions.Current.Humidity =
                        singleNode1.Attributes[
                            "data"].InnerText;
            var xmlNode1 = xmlConditions.SelectSingleNode("xml_api_reply/weather/current_conditions/wind_condition");
            if (xmlNode1 != null)
                if (xmlNode1.Attributes != null)
                    conditions.Current.WindSpeedM =
                        xmlNode1.Attributes
                            ["data"].InnerText;

            if (xmlConditions.SelectSingleNode("xml_api_reply/weather/problem_cause") != null)
            {
                conditions = null;
            }
            else
            {
                XmlNodeList list = xmlConditions.SelectNodes("/xml_api_reply/weather");
                foreach(XmlNode node1 in list)
                {
                    XmlNodeList days = node1.SelectNodes("forecast_conditions");
                    foreach (XmlNode doo in days)
                    {
                        string dow = doo.SelectSingleNode("day_of_week").Attributes["data"].InnerText;
                        switch (dow)
                        {
                            case "Mon":
                                forecast = d.Monday;
                                break;
                            case "Tue":
                                forecast = d.Tuesday;
                                break;
                            case "Wed":
                                forecast = d.Wednesday;
                                break;
                            case "Thu":
                                forecast = d.Thursday;
                                break;
                            case "Fri":
                                forecast = d.Friday;
                                break;
                            case "Sat":
                                forecast = d.Saturday;
                                break;
                            case "Sun":
                                forecast = d.Sunday;
                                break;
                        }
                        forecast.WeatherText = doo.SelectSingleNode("condition").Attributes["data"].InnerText;
                        forecast.TempHiF = doo.SelectSingleNode("high").Attributes["data"].InnerText;                         
                    }
                }       
            }
            }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 C# 中使用天气 api [关闭] 的相关文章

随机推荐

  • 使用 C# 将 excel 转换为 JSON,并将 excel 中的第一行作为键

    我正在寻找从中读取数据excel 2010通过指定工作表名称 然后我想将数据转换为JSON格式 假设我有一个这样的 Excel 工作表 Name Age Country Waugh Timothy 10 UK Freeman Neil 20
  • JavaFX 3-D 条形图与 Java 8

    是否有使用现代 3 D API 的最新 Java 8 的 3 D 条形图示例 我想使用 Java 8 中的 3 D API JavaFX 的早期版本中有一个 3d Bar Chart 演示 但已被删除 您可以自己创建条形图 只需创建一个网格
  • 在 Silverlight 中迭代枚举?

    在 Net 中 可以使用以下方式迭代枚举 System Enum GetNames typeof MyEnum or System Enum GetValues typeof MyEnum 然而 在 Silverlight 3 中 未定义
  • Mobaxterm:如何防止 ssh 会话退出?

    我该如何阻止这个 Session stopped Press
  • SwiftUI 拖放文件

    我正在尝试向我的 SwiftUI Mac 应用程序添加 拖放 手势 功能 我想将文件从我的系统 桌面拖放到我的应用程序中 我发现在常规 Swift 中这是可能的 我现在正在尝试在 SwiftUI 中执行此操作 我找到一个onDrop Swi
  • UITableView didSelectRowAt 不称为 iOS 10,但适用于 9.3.5

    关于这个问题有很多问题 但到目前为止我找到的答案并不适用 在这种情况下 该表在 iOS 9 3 5 中正常工作 但不适用于 iOS 10 或 10 3 1 我已经排除了 没有正确设置委托 代表执行 3 个功能 didSelectRowAt
  • 我应该为所有内容编写单元测试吗?

    我想知道我是否应该为所有内容编写单元测试 有一些类很难编写单元测试 例如 我正在编写一些处理音频的程序 用于从麦克风捕获音频的类和用于向扬声器播放音频的类 我如何为这些类编写单元测试 我无法获得这些类的输出和输入 因此几乎不可能测试它们 我
  • 退出在子子程序中打开的 Word.Application

    我们宣布Word Application对象在Pro1下面的子函数 因为我们需要返回Word Document对象 应保持打开状态 我怎样才能退出父子例程中的Word应用程序 test01 我需要objWord声明于的对象Pro1函数将在没
  • Android SDK 中的虚拟摇杆

    我想在我的 Android 应用程序中制作一个虚拟操纵杆来控制遥控汽车 我怎样才能做到这一点 我可以使用 API 来执行此操作吗 我在网上查看的代码示例似乎不起作用 你可以看一下这个 很简单 有文档等 https github com co
  • 如何在 Visual Studio 2008 或 Visual Studio 2010 中设置 JavaScript 断点

    我正在尝试使用 Visual Studio 2010 调试 JavaScript 代码 但无法设置断点 我怎样才能做到这一点 我只是注意到了一些事情 每次我尝试调用一个函数 无论什么函数 在 JavaScript 中 不知何故jQuery
  • 如何通过命令行对 apk 进行签名

    请注意 我们已经在 Android SDK 的帮助下通过命令行创建了 apk 文件 现在 由于将其上传到 Google Play 商店 需要对 apk 进行签名 我们该怎么做呢 首先 您需要一个密钥库来开始该过程 您将使用此密钥库对您的 a
  • 使用 SFINAE 和 void_t 区分类型

    我遇到了一些情况 我必须编写两个函数 其中一个应该使用原始类型调用 std string 另一种应该用其他类型来调用 到目前为止 我以工作解决方案结束 template
  • 为什么 Google Appengine 连接到 CloudSQL 的速度这么慢

    当连接到 CloudSQL 后端时 我发现开发和生产之间的延迟存在巨大差异 比我预期的要大得多 我进行了一个测试 其中 我获取了 125 250 500 1000 和 2000 行 行大小约为 30 字节 我获取了每行大小 20 次 以获得
  • 如何对电子邮件发送进行单元测试?

    我想使用 NET C 框架或任何兼容的库测试我的电子邮件发送功能 有什么建议吗 如果您只需要测试发送电子邮件 您可以像这样配置您的 config 文件
  • java.lang.ClassCastException:org.glassfish.jersey.servlet.ServletContainer 无法转换为 javax.servlet.Servlet

    由于以下错误 我无法在 Tomcat 7 0 服务器上启动 Web 应用程序 java lang ClassCastException org glassfish jersey servlet ServletContainer cannot
  • 二叉搜索树中的中序遍历复杂性(使用迭代器)?

    相关问题 二叉树中序树遍历的时间复杂度O N https stackoverflow com questions 9658700 time complexity of inorder tree traversal of binary tre
  • 我们在 Get-Azure Resource -ApiVersion 参数中放入什么?

    我正在尝试获取特定的 Microsoft Azure 网站资源 以便我可以更改其某些属性 为了获得单一资源 Get AzureResource需要ApiVersion财产 我在哪里可以找到这个 参数集 获取单个资源 Get AzureRes
  • 如何使用相同的函数对 C 中的字符串进行异或加扰并再次返回?

    我正在尝试混淆程序中的字符串 目前 我只有一个简单的字符串反转工作 我希望能够对数据执行 XOR 加扰以使其更加安全 但是我尝试过的方法不起作用 使用相同的函数和输入类型来解码字符串 这对于字符串反转来说没有问题 因为它只是反转回来 但是可
  • Sails/Waterline 中的软删除

    尝试使用以下方法删除用户模型 Hard Delete User destroy id userId function err res Hard Delete 我需要对用户模型进行软删除 并且当前在删除和更新文档时将标志 isDeleted
  • 如何在 C# 中使用天气 api [关闭]

    Closed 这个问题是无关 help closed questions 目前不接受答案 我想实现天气信息 它将根据我的经度和纬度向我显示结果 我的应用程序从 GPS 获取坐标 因此获取它们不是问题 唯一的问题是 我想显示离我最近的城市的一