导航

  1. 常用CHM
  2. 订阅
  • 本站大部分内容从网上收集,收集目的仅供研究、学习。涉及版权或不希望收录您的文章请您及时与我联系。
  • 本站IM群,请自行选择。请各位朋友按照自己喜好加入。加入群后请及时发言,防止被清理。谢谢您的合作!!!
  • QQ群:Y①WEB开发(ASP.NET)号码:7351660 QQ群:Y②WEB开发(ASP+.NET)号码:11864905
  • QQ群:Y③WEB开发(DIV+CSS)号码:16610506 QQ群:Y④WEB开发(JS+AJAX)号码:16143998
  • QQ群:Y⑤WEB开发(新手)号码:12777715 MSN群:yaosansi[at]126.com
« Squid 中文权威指南-第9章-Cache拦截(彭勇华)Subsonic介绍及使用 »

使用Google Weather API查询天气预报

分类: WebSite 发布: yaosansi 浏览: 日期: 2008年8月22日

 

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?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;
 
///Code by Roman Alifanov - API by Google
///Posted on http://animaonline.blogspot.com
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=北京

相关文章:

  • quote

    回复该留言 4 liqwei

    2009/11/13 21:03:30 [Report Spam]

    看看我开发的这个接口:http://api.liqwei.com/ 采用中央气象台的数据,

    还可以利用 139.com 邮箱的邮件到达短信提醒的功能
    实现免费的天气短信,我们只要找到一个接口,能定时向指定 139.com 邮箱发送天气邮件就可以了,
    这里就有一个自己开发的接口,有兴趣的朋友可以试试:http://api.liqwei.com/weather/subscription.asp
    只要输入 139.com 邮箱地址,选择订阅的城市就可以,而且支持订阅多个城市的天气,
    自我感觉用起来还行,当然别的邮箱也可以,就是无法收到天气短信,哈哈

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

Powered By Z-Blog 1.8 Walle Build 100427

Copyright 2005-2010 yaosansi'site All Rights Reserved.
感谢系统大玩家为本站提供FTP空间
辽ICP备05021434号