·您当前的位置:首页 > 技术教程 > Flex技术 >

[Flex]用谷歌API做flash形式的天气查询代码

时间:2012-09-17 14:53cnblogs.com
[Flex]用谷歌API做flash形式的天气查询代码

[Flex]用谷歌API做flash形式的天气查询代码

  1. 代码   
  2.  
  3.  
  4.  
  5.  
  6. Code highlighting produced by Actipro CodeHighlighter (freeware)  
  7. http://www.CodeHighlighter.com/  
  8.  
  9. --><?xml version="1.0" encoding="utf-8"?> 
  10. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="loadData();" width="236" height="206"> 
  11.     <mx:Script> 
  12.         <![CDATA[  
  13.             import mx.controls.Label;  
  14.             import mx.controls.Alert;  
  15.             import flash.net.URLLoader;  
  16.             import flash.net.URLRequest;  
  17.             import flash.net.URLRequestMethod;  
  18.             import flash.net.URLVariables;  
  19.             import flash.system.Security;  
  20.             import mx.managers.CursorManager;  
  21.               
  22.             [Bindable]  
  23.             private var imgBase:String="http://www.google.com/";  
  24.             private var srcBase:String="http://www.google.com/ig/api";  
  25.             private var uLoader:URLLoader=new URLLoader;  
  26.             private var uReq:URLRequest=new URLRequest;  
  27.             private var variables:URLVariables=new URLVariables;  
  28.             [Bindable]  
  29.             private var dataXML:XML;  
  30.             private var alert:Canvas;  
  31.               
  32.             private function loadData(str:String="beijing"):void{  
  33.                 variables.hl="zh-cn";  
  34.                 variables.weather=str;  
  35.                 uReq.url=srcBase;  
  36.                 uReq.data=variables;  
  37.                 uReq.method=URLRequestMethod.GET;  
  38.                 uLoader.load(uReq);  
  39.                 uLoader.addEventListener(Event.COMPLETE,dataReady);  
  40.             }  
  41.               
  42.             private function dataReady(event:Event):void{  
  43.                 var tmp:XML=new XML(uLoader.data);  
  44.                 if(tmp.weather.current_conditions.toString()!="") dataXML=tmp;  
  45.                 else error();  
  46.             }  
  47.               
  48.             private function onEnter(event:KeyboardEvent):void{  
  49.                 if(event.charCode==13){  
  50.                     loadData(city.text);  
  51.                 }  
  52.             }  
  53.               
  54.             private function error():void{  
  55.                 if(!alert){  
  56.                     alert=new Canvas;  
  57.                     alert.graphics.beginFill(0x000000,0.5);  
  58.                     alert.graphics.drawRect(0,0,stage.width,stage.height);  
  59.                     var lb:Label=new Label;  
  60.                     lb.text="查询失败!";  
  61.                     lb.setStyle("color",0xFF0000);  
  62.                     lb.setStyle("fontSize",22);  
  63.                     lb.x=70,lb.y=80;  
  64.                     alert.addChild(lb);  
  65.                 }  
  66.                 addChild(alert);  
  67.                 setTimeout(function():void{removeChild(alert)},1000);  
  68.             }  
  69.         ]]> 
  70.     </mx:Script> 
  71.       
  72.     <mx:Style> 
  73.         Label{  
  74.             font-size:12;  
  75.         }  
  76.         .red{  
  77.             color:#FF0000;  
  78.             font-size:16;  
  79.         }  
  80.         ControlBar{  
  81.             padding-top:5;  
  82.         }  
  83.         Button{  
  84.             font-size:12;  
  85.             padding-left:0;  
  86.             padding-top:0;  
  87.             padding-right:0;  
  88.             padding-bottom:0;  
  89.         }  
  90.         ToolTip{  
  91.             font-size:12;  
  92.         }  
  93.     </mx:Style> 
  94.       
  95.     <mx:TitleWindow title="{dataXML.weather.forecast_information.city.@data}" x="0" y="0" width="234" height="204" layout="absolute"> 
  96.         <mx:TabNavigator x="6" y="5" width="202" height="129"> 
  97.             <mx:Canvas id="a1" label="{dataXML.weather.forecast_conditions[0].day_of_week.@data}" width="100%" height="100%"> 
  98.                 <mx:Image x="10" y="0" width="50" height="50" source="{imgBase+dataXML.weather.forecast_conditions[0].icon.@data}"/> 
  99.                 <mx:Label styleName="red" x="68" y="-3" width="132" height="21" text="{dataXML.weather.forecast_conditions[0].condition.@data}"/> 
  100.                 <mx:Label x="10" y="55" text="最底温度:" width="70"/> 
  101.                 <mx:Label x="10" y="73" text="最高温度:" width="70"/> 
  102.                 <mx:Label x="72" y="55" width="118" text="{dataXML.weather.forecast_conditions[0].low.@data+' ℃'}"/> 
  103.                 <mx:Label x="72" y="73" width="118" text="{dataXML.weather.forecast_conditions[0].high.@data+' ℃'}"/> 
  104.                 <mx:Label x="68" y="17" text="{dataXML.weather.current_conditions.humidity.@data}" width="132"/> 
  105.                 <mx:Label x="68" y="35" text="{dataXML.weather.current_conditions.wind_condition.@data}" width="132"/> 
  106.             </mx:Canvas> 
  107.             <mx:Canvas id="a2" label="{dataXML.weather.forecast_conditions[1].day_of_week.@data}" width="100%" height="100%"> 
  108.                 <mx:Image x="10" y="0" width="50" height="50" source="{imgBase+dataXML.weather.forecast_conditions[1].icon.@data}"/> 
  109.                 <mx:Label styleName="red" x="68" y="6" width="118" height="37" text="{dataXML.weather.forecast_conditions[1].condition.@data}"/> 
  110.                 <mx:Label x="10" y="55" text="最底温度:" width="70"/> 
  111.                 <mx:Label x="10" y="73" text="最高温度:" width="70"/> 
  112.                 <mx:Label x="72" y="55" width="118" text="{dataXML.weather.forecast_conditions[1].low.@data+' ℃'}"/> 
  113.                 <mx:Label x="72" y="73" width="118" text="{dataXML.weather.forecast_conditions[1].high.@data+' ℃'}"/> 
  114.             </mx:Canvas> 
  115.             <mx:Canvas id="a3" label="{dataXML.weather.forecast_conditions[2].day_of_week.@data}" width="100%" height="100%"> 
  116.                 <mx:Image x="10" y="0" width="50" height="50" source="{imgBase+dataXML.weather.forecast_conditions[2].icon.@data}"/> 
  117.                 <mx:Label styleName="red" x="68" y="6" width="118" height="37" text="{dataXML.weather.forecast_conditions[2].condition.@data}"/> 
  118.                 <mx:Label x="10" y="55" text="最底温度:" width="70"/> 
  119.                 <mx:Label x="10" y="73" text="最高温度:" width="70"/> 
  120.                 <mx:Label x="72" y="55" width="118" text="{dataXML.weather.forecast_conditions[2].low.@data+' ℃'}"/> 
  121.                 <mx:Label x="72" y="73" width="118" text="{dataXML.weather.forecast_conditions[2].high.@data+' ℃'}"/> 
  122.             </mx:Canvas> 
  123.             <mx:Canvas id="a4" label="{dataXML.weather.forecast_conditions[3].day_of_week.@data}" width="100%" height="100%"> 
  124.                 <mx:Image x="10" y="0" width="50" height="50" source="{imgBase+dataXML.weather.forecast_conditions[3].icon.@data}"/> 
  125.                 <mx:Label styleName="red" x="68" y="6" width="118" height="37" text="{dataXML.weather.forecast_conditions[3].condition.@data}"/> 
  126.                 <mx:Label x="10" y="55" text="最底温度:" width="70"/> 
  127.                 <mx:Label x="10" y="73" text="最高温度:" width="70"/> 
  128.                 <mx:Label x="72" y="55" width="118" text="{dataXML.weather.forecast_conditions[3].low.@data+' ℃'}"/> 
  129.                 <mx:Label x="72" y="73" width="118" text="{dataXML.weather.forecast_conditions[3].high.@data+' ℃'}"/> 
  130.             </mx:Canvas> 
  131.         </mx:TabNavigator> 
  132.         <mx:TextInput width="174.5" height="22" x="6" y="138" id="city" keyUp="onEnter(event)"/> 
  133.         <mx:Button width="22" label="搜" x="185" y="137" click="loadData(city.text)"/> 
  134.     </mx:TitleWindow> 
  135. </mx:Application> 

截图:

/Files/huomiao/天气.rar

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器