GoogleWeatherAPI非官方文档
转载:http://blog.sina.com.cn/s/blog_6322061e0100jxt7.html
<wbr><wbr><wbr>前一段时间开始,因为公司项目需要,一直在研究Google Weather API。</wbr></wbr></wbr>
<wbr><wbr><wbr>因为Google Weather API并不是Google官方的API,所以Google并没有提供官方的文档,网上的关于Google Weather API的中文文章大多千篇一律,不知道被转载过多少次,英文文章中有些不错的文章,但是总得来说关于Google Weather API的详细的文章并不多见,所以决定提笔写这篇文章。<br> -----------------------------------------我是华丽的分割线-----------------------------------------<br> 基础篇<br><wbr><wbr><wbr>首先是如何调用Google Weather API,如果你在Google里搜索能够搜索出以下的内容,但是完全没有原始来源,看上去是很简单,说明也还算详细。<br><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; line-height:21px; color:rgb(0,0,0)">1.邮政编码法:(</span>只支持美国地区使用邮政编码进行查询<span style="word-wrap:normal; word-break:normal; line-height:21px; color:rgb(0,0,0)">)</span></wbr></wbr></wbr></span><br style="font-style:italic; color:rgb(0,0,0)"><em><wbr><wbr><wbr></wbr></wbr></wbr></em><a href="http://www.google.com/ig/api?hl=zh-cn&weather=94043" style="text-decoration:none; color:rgb(0,0,0); font-style:italic">http://www.google.com/ig/api?hl=zh-cn&weather=94043</a><br style="font-style:italic; color:rgb(0,0,0)"><em><wbr><wbr><wbr>(94043 为 山景城, 美国加州 的邮政编码)</wbr></wbr></wbr></em><br style="font-style:italic; color:rgb(0,0,0)"><em><wbr><wbr>2.经纬度坐标作法:</wbr></wbr></em><br style="font-style:italic; color:rgb(0,0,0)"><em><wbr><wbr><wbr></wbr></wbr></wbr></em><a href="http://www.google.com/ig/api?hl=zh-cn&weather=,,,30670000,104019996" target="_blank" style="text-decoration:none; color:rgb(62,115,160)"></a><a href="http://www.google.com/ig/api?hl=zh-cn&weather=,,,30670000,104019996" style="text-decoration:none; color:rgb(0,0,0); font-style:italic">http://www.google.com/ig/api?hl=zh-cn&weather=,,,30670000,104019996</a><br style="font-style:italic; color:rgb(0,0,0)"><em><wbr><wbr><wbr>(30670000,104019996 为 成都, 中国大陆 的经纬度坐标)</wbr></wbr></wbr></em><br style="font-style:italic; color:rgb(0,0,0)"><em><wbr><wbr><wbr>3.城市名称法:</wbr></wbr></wbr></em><br style="font-style:italic; color:rgb(0,0,0)"><em><wbr><wbr><wbr></wbr></wbr></wbr></em><a href="http://www.google.com/ig/api?weather=Beijing" style="text-decoration:none; color:rgb(0,0,0); font-style:italic">http://www.google.com/ig/api?weather=Beijing</a><br style="color:rgb(0,0,0)"><em><wbr><wbr><wbr>附:</wbr></wbr></wbr></em><br style="font-style:italic; color:rgb(0,0,0)"><em><wbr><wbr><wbr>支持的国家代码列表:</wbr></wbr></wbr></em><br style="font-style:italic; color:rgb(0,0,0)"><em><wbr><wbr><wbr></wbr></wbr></wbr></em><a href="http://www.google.com/ig/countries?output=xml&hl=zh-cn" target="_blank" style="text-decoration:none; color:rgb(62,115,160)"></a><a href="http://www.google.com/ig/countries?output=xml&hl=zh-cn" style="text-decoration:none; color:rgb(0,0,0); font-style:italic">http://www.google.com/ig/countries?output=xml&hl=zh-cn</a><br style="font-style:italic; color:rgb(0,0,0)"><em><wbr><wbr><wbr>(查询 Google 所支持的所有国家的代码,并以 zh-cn 简体中文显示)</wbr></wbr></wbr></em><br style="font-style:italic; color:rgb(0,0,0)"><em><wbr><wbr><wbr>支持的城市代码列表:</wbr></wbr></wbr></em><br style="font-style:italic; color:rgb(0,0,0)"><em><wbr><wbr><wbr></wbr></wbr></wbr></em><a href="http://www.google.com/ig/cities?output=xml&hl=zh-cn&country=cn" target="_blank" style="text-decoration:none; color:rgb(62,115,160)"></a><a href="http://www.google.com/ig/cities?output=xml&hl=zh-cn&country=cn" style="text-decoration:none; color:rgb(0,0,0); font-style:italic">http://www.google.com/ig/cities?output=xml&hl=zh-cn&country=cn</a><br><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; line-height:21px; color:rgb(255,0,0)">其实以上有很多地方是错误的。</span><br><wbr><wbr><wbr>一、邮编法,支持的国家包括美国,加拿大,德国,英国,法国等,不仅仅是美国,以下是一些其他国家的例子,具体可以去尝试各种不同的国家邮编。<br><wbr><wbr><wbr>加拿大多伦多<a href="http://www.google.com/ig/api?weather=M4B1G5" target="_blank" style="text-decoration:none; color:rgb(62,115,160)">http://www.google.com/ig/api?weather=M4B1G5</a><br><wbr><wbr><wbr>德国柏林<a href="http://www.google.com/ig/api?weather=10115,DE" target="_blank" style="text-decoration:none; color:rgb(62,115,160)">http://www.google.com/ig/api?weather=10115,DE</a>(应该是和美国重复的关系需要加DE)<br><wbr><wbr><wbr>法国巴黎<a href="http://www.google.com/ig/api?weather=75008" target="_blank" style="text-decoration:none; color:rgb(62,115,160)">http://www.google.com/ig/api?weather=75008</a><br><wbr><wbr><wbr>英国南安普顿<a href="http://www.google.com/ig/api?weather=SO163UB" target="_blank" style="text-decoration:none; color:rgb(62,115,160)">http://www.google.com/ig/api?weather=SO163UB</a><br><wbr><wbr><wbr>反正不支持天朝,Google退出中国后,应该也不会有对于中国邮编的支持。<br><wbr><wbr><wbr>二、经纬度法,没啥好说的,除了和Google Map API的经纬度用的单位不同需要转换,其他还是ok,可以和Google Map无缝衔接。<br><wbr><wbr><wbr>三、城市名法,默认只支持英文,如果输入中文的话可以用Google Language API翻译成英文再查询,国际化本地化处理还是很方便的,要另它支持本国文字请看高级篇。<br><wbr><wbr><wbr>四、支持国家和城市列表请无视,实在是不够全。<br><wbr><wbr><wbr>标准返回XML形式(现在的温度+今天的详细预报+后三天的简略预报)<br><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"><xml_api_reply version="1"></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"><weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0"></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"><forecast_information></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"><city data="Shanghai, Shanghai"/></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"><postal_code data="shanghai"/></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"><latitude_e6 data=""/></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"><longitude_e6 data=""/></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"><forecast_date data="2010-09-13"/></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"><current_date_time data="2010-09-14 06:00:00 +0000"/></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"><unit_system data="SI"/></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"></forecast_information></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"><current_conditions></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"><condition data="晴"/></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"><temp_f data="79"/></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"><temp_c data="26"/></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"><humidity data="湿度: 83%"/></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"><icon data="/ig/images/weather/sunny.gif"/></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"><wind_condition data="风向: 东、风速:4 米/秒"/></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"></current_conditions></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"><forecast_conditions></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"><day_of_week data="周一"/></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"><low data="24"/></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"><high data="29"/></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"><icon data="/ig/images/weather/cn_cloudy.gif"/></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"><condition data="多云"/></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"></forecast_conditions></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"><forecast_conditions></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"><day_of_week data="周二"/></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"><low data="24"/></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"><high data="29"/></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"><icon data="/ig/images/weather/chance_of_storm.gif"/></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"><condition data="可能有暴风雨"/></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"></forecast_conditions></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"><forecast_conditions></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"><day_of_week data="周三"/></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"><low data="24"/></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"><high data="32"/></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"><icon data="/ig/images/weather/chance_of_storm.gif"/></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"><condition data="可能有暴风雨"/></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"></forecast_conditions></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"><forecast_conditions></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"><day_of_week data="周四"/></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"><low data="25"/></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"><high data="30"/></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"><icon data="/ig/images/weather/chance_of_rain.gif"/></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"><condition data="可能有雨"/></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"></forecast_conditions></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"></weather></span><br style="font-style:italic"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-style:italic"></xml_api_reply></span><br> -----------------------------------------我是华丽的分割线-----------------------------------------<br> 高级应用篇<br><wbr><wbr><wbr>一、国际化本地化<br><wbr><wbr><wbr>hl 返回的语言文字(ISO639语言编码+国家缩写,如zh_CN,en_US等),不选的话默认会根据(操作系统?IP地址?)返回语言,就我本人来看切换浏览器语言返回的都是中文。<br><wbr><wbr><wbr>ie 输入文字编码(浏览器语言编码) 有了这个参数就可以使用下面这样的查询<br><wbr><wbr><wbr><a href="http://www.google.com/ig/api?weather=%E4%B8%8A%E6%B5%B7&hl=zh_CN&ie=utf-8&oe=gb2312" style="text-decoration:none; color:rgb(62,115,160)">http://www.google.com/ig/api?weather=上海&hl=zh_CN&ie=gb2312</a><br><wbr><wbr><wbr>oe 输出文字编码(浏览器语言编码) 这个主要是为了控制输出内容能正常显示的编码<br><wbr><wbr><wbr><a href="http://www.google.com/ig/api?weather=%E4%B8%8A%E6%B5%B7&hl=zh_CN&ie=gb2312&oe=gb2312" style="text-decoration:none; color:rgb(62,115,160)">http://www.google.com/ig/api?weather=上海&hl=zh_CN&ie=gb2312&oe=gb2312</a><br><wbr><wbr><wbr>output 输出格式(xml,json)只有少部分数据能返回json值...这一点远不如Twitter和Facebook的API来得好...<br><wbr><wbr><wbr>二、多城市查询<br><wbr><wbr><wbr>weather 城市名/经纬度/邮政编码 最神奇的就是这个参数了,实际上你可以传多个weather参数来返回城市信息。(某同事尝试出来的)比如<br><wbr><wbr><wbr><a href="http://www.google.com/ig/api?weather=%E4%B8%8A%E6%B5%B7&weather=%E5%8C%97%E4%BA%AC&hl=zh_CN&ie=gb2312&oe=gb2312" style="text-decoration:none; color:rgb(62,115,160)">http://www.google.com/ig/api?weather=上海&weather=北京&hl=zh_CN&ie=gb2312&oe=gb2312</a><br><wbr><wbr><wbr>其实实际运用并不这么简单,Google提供了各种api可以一起拼接在一起...非常佩服Google的神人...<br><wbr><wbr><wbr><a href="http://www.google.com/ig/api?movies=new%20york&weather=%E4%B8%8A%E6%B5%B7&stock=Google&hl=zh_CN&ie=gb2312&oe=gb2312" style="text-decoration:none; color:rgb(62,115,160)">http://www.google.com/ig/api?movies=new%20york&weather=上海&stock=Google&hl=zh_CN&ie=gb2312&oe=gb2312</a><br> -----------------------------------------我是华丽的分割线-----------------------------------------<br> 实际运用篇<br><wbr><wbr><wbr>一、调用方法<br><wbr><wbr><wbr>因为Google这一系列的API都在Google.com这个域下面,跨域请求XML的话没有啥很好的办法,Google域的http://www.google.com/crossdomain.xml又非常严格,非公开的API完全不支持跨域,一般都是自己服务器做个代理。YUI里有个很好的例子(前端YUI到后端PHP代码都有)<a href="http://developer.yahoo.com/yui/3/examples/io/io-weather.html" target="_blank" style="text-decoration:none; color:rgb(62,115,160)">http://developer.yahoo.com/yui/3/examples/io/io-weather.html</a>。不过其中调用的是Yahoo Weather API,稍微修改下就能用在Google Weather API上了,具体实现并不难,无论是web应用还是iPhone,android的应用,使用Google Weather API的很多。<br><wbr><wbr><wbr>二,异常情况<br><wbr><wbr><wbr>因为Google Weather API并不是公开的,所以很多异常都会出现,某些重要城市的天气找不到,比如天津一天总有几个小时找不到温度情况,或者返回的温度或者天气状况会是空值,或者干脆一段时间不更新天气信息之类种种,或者说预报温度不是很准等等,具体可以去看Google的投诉版...<br><wbr><wbr><wbr>三、其他API<br><wbr><wbr><wbr>Yahoo Weather API本身比Google Weather API的数据结果强大,不过国际化本地化是个大问题,最重要的是Yahoo Weather API是不能从事商业用途的。当然你想用在天朝也很正常,知识产权版权一向是被我们所忽视的。我只看到过iphone, ipod, android的官方天气运用使用这个(估计是付钱的),其他的国内的一些实在是不清楚来源不敢用。<br><wbr><wbr><wbr>大概就是这么多,分享出来给大家。</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>