DFdou's Blog Life is short,Be yourself.

1611/096

Air-fWeather Google Weather Api使用例子

需要Flash Player9+,如果你看到这段文字,请点击下边的图片链接更新您的Flash Player,或者直接下载Air文件进行安装。
get_flash_player

AIR程序下载地址(需要先安装AIR运行时):http://nwhy.org/nwhy/fDEV/fWeather.air.
fWeather
不知道为什么,Google Weather Api的说明文档一直没有找到,难道现在连Lab状态都不是?
这次是用Flash CS4做的AIR程序,发布的时候发现Flash在生成xml配置文件的时候存在一点问题啊……

Google Weather Api的使用非常的简单,下边是主要代码:

private function cityLoadComplete(_e:Event):void {
	var cityXML:XML=XML(_e.target.data);
	cityXML.ignoreWhitespace=true;
	var len:uint=cityXML.cities.city.length();
	for (var i:int=0; i<len ; i++) {
		cbx_city.addItem({ label: cityXML.cities.city[i].name.@data, data:cityXML.cities.city[i].name.@data });
	}
}

private function cityChange(_e:Event):void {
	var cbx:ComboBox=_e.currentTarget as ComboBox;
	var url_selectedCity:String=cbx.selectedItem.label;
	var loader:URLLoader = new URLLoader();
	loader.addEventListener(Event.COMPLETE, weatherLoadComplete);
	loader.addEventListener(ProgressEvent.PROGRESS,loadingProgress);
	loader.addEventListener(IOErrorEvent.IO_ERROR, loadIOError);
	loader.load(new URLRequest(url_weather+url_selectedCity));
}
private function weatherLoadComplete(_e:Event) {
	var weatherXML:XML=XML(_e.target.data);
	weatherXML.ignoreWhitespace=true;
	for (var i:uint=0; i<num_weather; i++) {
		trace(weatherXML.weather.forecast_conditions[i].day_of_week.@data);
		View_weatherV0(sp_weatherCont.getChildAt(i)).tf_date.text=weatherXML.weather.forecast_conditions[i].day_of_week.@data;
		View_weatherV0(sp_weatherCont.getChildAt(i)).mc_img.source=url_google+weatherXML.weather.forecast_conditions[i].icon.@data;
		View_weatherV0(sp_weatherCont.getChildAt(i)).tf_temp.text="温度"+weatherXML.weather.forecast_conditions[i].low.@data+"/"+weatherXML.weather.forecast_conditions[i].high.@data;
		View_weatherV0(sp_weatherCont.getChildAt(i)).tf_cond.text=weatherXML.weather.forecast_conditions[i].condition.@data;
	}
}

至于Google那边返回城市的xml格式,差不多是这个样子:

<xml_api_reply version="1">
  <cities>
    <city>
      <name data="保定"/>
      <latitude_e6 data="38849998"/>
      <longitude_e6 data="115569999"/>
    </city>
    <city default="true">
      <name data="北京"/>
      <latitude_e6 data="39930000"/>
      <longitude_e6 data="116279998"/>
    </city>
    <city>
...
</city></cities>

然后是天气的xml,差不多是这个样子:

<xml_api_reply version="1">
  <weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0">
    <forecast_information>
      <city data="Changchun, Jilin"/>
      <postal_code data="长春"/>
      <latitude_e6 data=""/>
      <longitude_e6 data=""/>
      <forecast_date data="2009-11-15"/>
      <current_date_time data="2009-11-16 01:00:00 +0000"/>
      <unit_system data="SI"/>
    </forecast_information>
    <current_conditions>
      <condition data="多云"/>
      <temp_f data="10"/>
      <temp_c data="-12"/>
      <humidity data="湿度: 85%"/>
      <icon data="/ig/images/weather/mostly_cloudy.gif"/>
      <wind_condition data="风向: 西、风速:2 米/秒"/>
    </current_conditions>
    <forecast_conditions>
      <day_of_week data="周日"/>
      <low data="-17"/>
      <high data="-7"/>
      <icon data="/ig/images/weather/mostly_sunny.gif"/>
      <condition data="晴间多云"/>
    </forecast_conditions>
    <forecast_conditions>
      <day_of_week data="周一"/>
      <low data="-19"/>
      <high data="-7"/>
      <icon data="/ig/images/weather/mostly_sunny.gif"/>
      <condition data="以晴为主"/>
    </forecast_conditions>
    <forecast_conditions>
      <day_of_week data="周二"/>
      <low data="-18"/>
      <high data="-8"/>
      <icon data="/ig/images/weather/sunny.gif"/>
      <condition data="晴"/>
    </forecast_conditions>
    <forecast_conditions>
      <day_of_week data="周三"/>
      <low data="-16"/>
      <high data="-4"/>
      <icon data="/ig/images/weather/sunny.gif"/>
      <condition data="晴"/>
    </forecast_conditions>
  </weather>
  <ignorewhitespace>true</ignorewhitespace>
</xml_api_reply>

对应着做解析,然后把数据给View就可以了,呵呵,还是挺轻松的吧。
本来打算做一个常用城市的保存功能,不过想想又懒的做哈哈。SO,就这样吧。

Some Related Posts

Comments (6) Trackbacks (0)
  1. :leilei 今天天气真冷!

    [Reply]

  2. 哈哈,过来看看

    [Reply]

  3. 能把这个小程序的源码发给我一下吗?有个小问题弄不明白,我的email是cido@qq.com,能发给我一下吗?谢谢

    [Reply]

    DFdou Reply:

    @mcxiaoke, :chaocai 发你了。

    [Reply]

  4. 博主,您好,请教您一个Google Weather Api的问题:
    我用Google Weather Api获得的xml数据跟igoogle桌面显示的不一样,我访问的路径是:
    http://www.google.com/ig/api?hl=zh-cn&weather=guangzhou,
    不知道是不是我用的api错了呢?

    [Reply]

    DFdou Reply:

    @soluo, 是这个没错,这个数据的确是乱七八糟的,不是很准确~

    [Reply]


Leave a comment

:zhuang :xizao :wuwu :wenhao :wc :touxiang :shuaya :shuajian :shengtian :paopao :no :meinv :maonv :loveu :leilei :kua :jrjr :dahan :chi :chaocai :byebye


CommentLuv Enabled

No trackbacks yet.