惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
Check Point Blog
J
Java Code Geeks
H
Hackread – Cybersecurity News, Data Breaches, AI and More
D
Docker
腾讯CDC
The GitHub Blog
The GitHub Blog
大猫的无限游戏
大猫的无限游戏
Microsoft Security Blog
Microsoft Security Blog
GbyAI
GbyAI
Stack Overflow Blog
Stack Overflow Blog
博客园 - 司徒正美
T
The Blog of Author Tim Ferriss
Vercel News
Vercel News
P
Proofpoint News Feed
雷峰网
雷峰网
博客园_首页
B
Blog RSS Feed
Microsoft Azure Blog
Microsoft Azure Blog
爱范儿
爱范儿
V
V2EX
F
Fortinet All Blogs
酷 壳 – CoolShell
酷 壳 – CoolShell
MyScale Blog
MyScale Blog
S
SegmentFault 最新的问题

博客园 - Yoshow

网络共享疑难解决方法 C#中如何一句代码交换a,b的值,都是int类型 URL编码表 - Yoshow - 博客园 无法在Web服务器上启动调试 Silverlight 中读取JSON数据 Silverlight 全屏方法 WebBrowser的Cookie操作 URL与URI的区别 Silverlight 3 Tools 中文版地址 fckeditor 修改记录 Path.Combine 使用方法 - Yoshow - 博客园 SharePoint 2007 SDK v1.5 C#中使用位运算来实现权限管理 Silverlight 3 正式版 开发工具 TFS 使用命令强行解锁被锁定的文件或文件夹 SharePoint磁盘容量规划 清理SQL Server 数据库日志 SharePoint 2007 SP2 SQL SERVER 2008 阻止保存要求重新创建表的更改
使用Google Weather API查询天气预报
Yoshow · 2009-07-03 · via 博客园 - Yoshow

使用Google Weather API查询天气预报

Google Weather API 只支持美国地区使用邮政编码进行查询,例如:
http://www.google.com/ig/api?hl=zh-cn&weather=94043
(94043 为 山景城, 美国加州 的邮政编码)

而除了美国以外的地区需要使用经纬度坐标作为参数才能执行 Google Weather API, 例如:
http://www.google.com/ig/api?hl=zh-cn&weather=,,,30670000,104019996
(30670000,104019996 为 成都, 中国大陆 的经纬度坐标)

当然,也可能通行城市名称的汉语拼音来查询,例如:以下是北京的天气
http://www.google.com/ig/api?hl=zh-cn&weather=Beijing

要其它地区的经纬度坐标,可以通过 Google API 提供的国家代码列表及相应的城市经纬度坐标列表可以查询到,以下是 Google API 提供的查询参数:
http://www.google.com/ig/countries?output=xml&hl=zh-cn

(查询 Google 所支持的所有国家的代码,并以 zh-cn 简体中文显示)
http://www.google.com/ig/cities?output=xml&hl=zh-cn&country=cn

C#范例:

using System;

using System.Collections.Generic;

using System.Text;

using System.Xml;

using System.Net;

namespace GoogleWeatherAPI_Parser

{

class Program

{

 static void Main(string[] args)

 {

     int i = 0;

     XmlNodeList GWP_NodeList = GoogleWeatherAPI_Parser(@"http://www.google.co.uk/ig/api?weather=Drammen").SelectNodes("xml_api_reply/weather/current_conditions");

     Console.WriteLine("Current weather in Drammen: " + GWP_NodeList.Item(i).SelectSingleNode("temp_c").Attributes["data"].InnerText);

     Console.ReadLine();

 }

 private static XmlDocument GoogleWeatherAPI_Parser(string baseUrl)

 {

     HttpWebRequest GWP_Request;

     HttpWebResponse GWP_Response = null;

     XmlDocument GWP_XMLdoc = null;

     try

     {

         GWP_Request = (HttpWebRequest)WebRequest.Create(string.Format(baseUrl));

         GWP_Request.UserAgent = @"Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4";

         GWP_Response = (HttpWebResponse)GWP_Request.GetResponse();

         GWP_XMLdoc = new XmlDocument();

         GWP_XMLdoc.Load(GWP_Response.GetResponseStream());

     }

     catch (Exception ex)

     {

         Console.WriteLine(ex.Message);

     }

     GWP_Response.Close();

     return GWP_XMLdoc;

 }

 }

}

附:其它天气API

Yahoo:http://developer.yahoo.com/weather/

北京天气(可以通过搜索查找)

http://xml.weather.yahoo.com/forecastrss?p=CHXX0008&u=f

国内API:

北京天气

http://weather.all2rss.com/weatherrss.asp?City=北京