google API做的flash天气查询
代码如下:
代码
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="loadData();" width="236" height="206">
<mx:Script>
<![CDATA[
import mx.controls.Label;
import mx.controls.Alert;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;
import flash.system.Security;
import mx.managers.CursorManager;
[Bindable]
private var imgBase:String="http://www.google.com/";
private var srcBase:String="http://www.google.com/ig/api";
private var uLoader:URLLoader=new URLLoader;
private var uReq:URLRequest=new URLRequest;
private var variables:URLVariables=new URLVariables;
[Bindable]
private var dataXML:XML;
private var alert:Canvas;
private function loadData(str:String="beijing"):void{
variables.hl="zh-cn";
variables.weather=str;
uReq.url=srcBase;
uReq.data=variables;
uReq.method=URLRequestMethod.GET;
uLoader.load(uReq);
uLoader.addEventListener(Event.COMPLETE,dataReady);
}
private function dataReady(event:Event):void{
var tmp:XML=new XML(uLoader.data);
if(tmp.weather.current_conditions.toString()!="") dataXML=tmp;
else error();
}
private function onEnter(event:KeyboardEvent):void{
if(event.charCode==13){
loadData(city.text);
}
}
private function error():void{
if(!alert){
alert=new Canvas;
alert.graphics.beginFill(0x000000,0.5);
alert.graphics.drawRect(0,0,stage.width,stage.height);
var lb:Label=new Label;
lb.text="查询失败!";
lb.setStyle("color",0xFF0000);
lb.setStyle("fontSize",22);
lb.x=70,lb.y=80;
alert.addChild(lb);
}
addChild(alert);
setTimeout(function():void{removeChild(alert)},1000);
}
]]>
</mx:Script>
<mx:Style>
Label{
font-size:12;
}
.red{
color:#FF0000;
font-size:16;
}
ControlBar{
padding-top:5;
}
Button{
font-size:12;
padding-left:0;
padding-top:0;
padding-right:0;
padding-bottom:0;
}
ToolTip{
font-size:12;
}
</mx:Style>
<mx:TitleWindow title="{dataXML.weather.forecast_information.city.@data}" x="0" y="0" width="234" height="204" layout="absolute">
<mx:TabNavigator x="6" y="5" width="202" height="129">
<mx:Canvas id="a1" label="{dataXML.weather.forecast_conditions[0].day_of_week.@data}" width="100%" height="100%">
<mx:Image x="10" y="0" width="50" height="50" source="{imgBase+dataXML.weather.forecast_conditions[0].icon.@data}"/>
<mx:Label styleName="red" x="68" y="-3" width="132" height="21" text="{dataXML.weather.forecast_conditions[0].condition.@data}"/>
<mx:Label x="10" y="55" text="最底温度:" width="70"/>
<mx:Label x="10" y="73" text="最高温度:" width="70"/>
<mx:Label x="72" y="55" width="118" text="{dataXML.weather.forecast_conditions[0].low.@data+' ℃'}"/>
<mx:Label x="72" y="73" width="118" text="{dataXML.weather.forecast_conditions[0].high.@data+' ℃'}"/>
<mx:Label x="68" y="17" text="{dataXML.weather.current_conditions.humidity.@data}" width="132"/>
<mx:Label x="68" y="35" text="{dataXML.weather.current_conditions.wind_condition.@data}" width="132"/>
</mx:Canvas>
<mx:Canvas id="a2" label="{dataXML.weather.forecast_conditions[1].day_of_week.@data}" width="100%" height="100%">
<mx:Image x="10" y="0" width="50" height="50" source="{imgBase+dataXML.weather.forecast_conditions[1].icon.@data}"/>
<mx:Label styleName="red" x="68" y="6" width="118" height="37" text="{dataXML.weather.forecast_conditions[1].condition.@data}"/>
<mx:Label x="10" y="55" text="最底温度:" width="70"/>
<mx:Label x="10" y="73" text="最高温度:" width="70"/>
<mx:Label x="72" y="55" width="118" text="{dataXML.weather.forecast_conditions[1].low.@data+' ℃'}"/>
<mx:Label x="72" y="73" width="118" text="{dataXML.weather.forecast_conditions[1].high.@data+' ℃'}"/>
</mx:Canvas>
<mx:Canvas id="a3" label="{dataXML.weather.forecast_conditions[2].day_of_week.@data}" width="100%" height="100%">
<mx:Image x="10" y="0" width="50" height="50" source="{imgBase+dataXML.weather.forecast_conditions[2].icon.@data}"/>
<mx:Label styleName="red" x="68" y="6" width="118" height="37" text="{dataXML.weather.forecast_conditions[2].condition.@data}"/>
<mx:Label x="10" y="55" text="最底温度:" width="70"/>
<mx:Label x="10" y="73" text="最高温度:" width="70"/>
<mx:Label x="72" y="55" width="118" text="{dataXML.weather.forecast_conditions[2].low.@data+' ℃'}"/>
<mx:Label x="72" y="73" width="118" text="{dataXML.weather.forecast_conditions[2].high.@data+' ℃'}"/>
</mx:Canvas>
<mx:Canvas id="a4" label="{dataXML.weather.forecast_conditions[3].day_of_week.@data}" width="100%" height="100%">
<mx:Image x="10" y="0" width="50" height="50" source="{imgBase+dataXML.weather.forecast_conditions[3].icon.@data}"/>
<mx:Label styleName="red" x="68" y="6" width="118" height="37" text="{dataXML.weather.forecast_conditions[3].condition.@data}"/>
<mx:Label x="10" y="55" text="最底温度:" width="70"/>
<mx:Label x="10" y="73" text="最高温度:" width="70"/>
<mx:Label x="72" y="55" width="118" text="{dataXML.weather.forecast_conditions[3].low.@data+' ℃'}"/>
<mx:Label x="72" y="73" width="118" text="{dataXML.weather.forecast_conditions[3].high.@data+' ℃'}"/>
</mx:Canvas>
</mx:TabNavigator>
<mx:TextInput width="174.5" height="22" x="6" y="138" id="city" keyUp="onEnter(event)"/>
<mx:Button width="22" label="搜" x="185" y="137" click="loadData(city.text)"/>
</mx:TitleWindow>
</mx:Application>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="loadData();" width="236" height="206">
<mx:Script>
<![CDATA[
import mx.controls.Label;
import mx.controls.Alert;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;
import flash.system.Security;
import mx.managers.CursorManager;
[Bindable]
private var imgBase:String="http://www.google.com/";
private var srcBase:String="http://www.google.com/ig/api";
private var uLoader:URLLoader=new URLLoader;
private var uReq:URLRequest=new URLRequest;
private var variables:URLVariables=new URLVariables;
[Bindable]
private var dataXML:XML;
private var alert:Canvas;
private function loadData(str:String="beijing"):void{
variables.hl="zh-cn";
variables.weather=str;
uReq.url=srcBase;
uReq.data=variables;
uReq.method=URLRequestMethod.GET;
uLoader.load(uReq);
uLoader.addEventListener(Event.COMPLETE,dataReady);
}
private function dataReady(event:Event):void{
var tmp:XML=new XML(uLoader.data);
if(tmp.weather.current_conditions.toString()!="") dataXML=tmp;
else error();
}
private function onEnter(event:KeyboardEvent):void{
if(event.charCode==13){
loadData(city.text);
}
}
private function error():void{
if(!alert){
alert=new Canvas;
alert.graphics.beginFill(0x000000,0.5);
alert.graphics.drawRect(0,0,stage.width,stage.height);
var lb:Label=new Label;
lb.text="查询失败!";
lb.setStyle("color",0xFF0000);
lb.setStyle("fontSize",22);
lb.x=70,lb.y=80;
alert.addChild(lb);
}
addChild(alert);
setTimeout(function():void{removeChild(alert)},1000);
}
]]>
</mx:Script>
<mx:Style>
Label{
font-size:12;
}
.red{
color:#FF0000;
font-size:16;
}
ControlBar{
padding-top:5;
}
Button{
font-size:12;
padding-left:0;
padding-top:0;
padding-right:0;
padding-bottom:0;
}
ToolTip{
font-size:12;
}
</mx:Style>
<mx:TitleWindow title="{dataXML.weather.forecast_information.city.@data}" x="0" y="0" width="234" height="204" layout="absolute">
<mx:TabNavigator x="6" y="5" width="202" height="129">
<mx:Canvas id="a1" label="{dataXML.weather.forecast_conditions[0].day_of_week.@data}" width="100%" height="100%">
<mx:Image x="10" y="0" width="50" height="50" source="{imgBase+dataXML.weather.forecast_conditions[0].icon.@data}"/>
<mx:Label styleName="red" x="68" y="-3" width="132" height="21" text="{dataXML.weather.forecast_conditions[0].condition.@data}"/>
<mx:Label x="10" y="55" text="最底温度:" width="70"/>
<mx:Label x="10" y="73" text="最高温度:" width="70"/>
<mx:Label x="72" y="55" width="118" text="{dataXML.weather.forecast_conditions[0].low.@data+' ℃'}"/>
<mx:Label x="72" y="73" width="118" text="{dataXML.weather.forecast_conditions[0].high.@data+' ℃'}"/>
<mx:Label x="68" y="17" text="{dataXML.weather.current_conditions.humidity.@data}" width="132"/>
<mx:Label x="68" y="35" text="{dataXML.weather.current_conditions.wind_condition.@data}" width="132"/>
</mx:Canvas>
<mx:Canvas id="a2" label="{dataXML.weather.forecast_conditions[1].day_of_week.@data}" width="100%" height="100%">
<mx:Image x="10" y="0" width="50" height="50" source="{imgBase+dataXML.weather.forecast_conditions[1].icon.@data}"/>
<mx:Label styleName="red" x="68" y="6" width="118" height="37" text="{dataXML.weather.forecast_conditions[1].condition.@data}"/>
<mx:Label x="10" y="55" text="最底温度:" width="70"/>
<mx:Label x="10" y="73" text="最高温度:" width="70"/>
<mx:Label x="72" y="55" width="118" text="{dataXML.weather.forecast_conditions[1].low.@data+' ℃'}"/>
<mx:Label x="72" y="73" width="118" text="{dataXML.weather.forecast_conditions[1].high.@data+' ℃'}"/>
</mx:Canvas>
<mx:Canvas id="a3" label="{dataXML.weather.forecast_conditions[2].day_of_week.@data}" width="100%" height="100%">
<mx:Image x="10" y="0" width="50" height="50" source="{imgBase+dataXML.weather.forecast_conditions[2].icon.@data}"/>
<mx:Label styleName="red" x="68" y="6" width="118" height="37" text="{dataXML.weather.forecast_conditions[2].condition.@data}"/>
<mx:Label x="10" y="55" text="最底温度:" width="70"/>
<mx:Label x="10" y="73" text="最高温度:" width="70"/>
<mx:Label x="72" y="55" width="118" text="{dataXML.weather.forecast_conditions[2].low.@data+' ℃'}"/>
<mx:Label x="72" y="73" width="118" text="{dataXML.weather.forecast_conditions[2].high.@data+' ℃'}"/>
</mx:Canvas>
<mx:Canvas id="a4" label="{dataXML.weather.forecast_conditions[3].day_of_week.@data}" width="100%" height="100%">
<mx:Image x="10" y="0" width="50" height="50" source="{imgBase+dataXML.weather.forecast_conditions[3].icon.@data}"/>
<mx:Label styleName="red" x="68" y="6" width="118" height="37" text="{dataXML.weather.forecast_conditions[3].condition.@data}"/>
<mx:Label x="10" y="55" text="最底温度:" width="70"/>
<mx:Label x="10" y="73" text="最高温度:" width="70"/>
<mx:Label x="72" y="55" width="118" text="{dataXML.weather.forecast_conditions[3].low.@data+' ℃'}"/>
<mx:Label x="72" y="73" width="118" text="{dataXML.weather.forecast_conditions[3].high.@data+' ℃'}"/>
</mx:Canvas>
</mx:TabNavigator>
<mx:TextInput width="174.5" height="22" x="6" y="138" id="city" keyUp="onEnter(event)"/>
<mx:Button width="22" label="搜" x="185" y="137" click="loadData(city.text)"/>
</mx:TitleWindow>
</mx:Application>
截图: