본문 바로가기

OM

Yahoo WebAPI를 이용한 날씨 위젯 1



WeatherWidget.mxml==================================================================================

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
  width="500" height="250" creationComplete="init()">
 <mx:Style>
  Application{
   fontSize : 12;
   backgroundColor : #ffffff;
  }
 </mx:Style>
 
 <mx:Script>
  <![CDATA[
   import com.yahoo.webapis.weather.*;
   import com.yahoo.webapis.weather.events.*;
   import mx.controls.*;
   
   private var weatherService:WeatherService;
   private var weather:Weather;
   
   private var locationArr:Array = [
     {label:"서울", code:"KSXX0037"},
     {label:"광주", code:"KSXX0014"},
     {label:"대구", code:"KSXX0026"},
     {label:"대전", code:"KSXX0027"},
     {label:"부산", code:"KSXX0050"},
     {label:"인천", code:"KSXX0009"},
     {label:"울산", code:"KSXX0029"},
     {label:"제주", code:"KSXX0004"},
    ];
   
   private function init():void
   {
    weatherService = new WeatherService();
    weather = new Weather();
   
    weatherService.addEventListener(WeatherResultEvent.WEATHER_LOADED, weatherResultEventHandler);
    weatherService.addEventListener(WeatherErrorEvent.INVALID_LOCATION, weatherErrorEventHandler);
    weatherService.addEventListener(WeatherErrorEvent.XML_LOADING, weatherErrorEventHandler);
    location_cb.dataProvider = locationArr;
   
    doSubmit();
   }
   
   private function doSubmit():void
   {
    weatherService.getWeather(location_cb.selectedItem.code, "c");
    bar.visible = true;
   }
   
   private function weatherResultEventHandler(e:WeatherResultEvent):void
   {
    weather = e.data as Weather;
    var current:CurrentConditions = weather.current;
    var forecast0:ForecastConditions = weather.forecast[0];
    var forecast1:ForecastConditions = weather.forecast[1];
   
    fore0Date_lb.text = dateFormatter.format(forecast0.date);
    fore0_img.source = forecast0.imageURL;
    fore0Description_lb.text = forecast0.description;
    fore0Temperature_lb.text = forecast0.high + "/" + forecast0.low;
   
    currentDate_lb.text = dateFormatter.format(weather.date);
    current_img.source = current.imageURL;
    currentDescription_lb.text = current.description;
    currentTemperature_lb.text = current.temperature.toString();
   
    fore1Date_lb.text = dateFormatter.format(forecast1.date);
    fore1_img.source = forecast1.imageURL;
    fore1Description_lb.text = forecast1.description;
    fore1Temperature_lb.text = forecast1.high + "/" + forecast1.low;
   
    bar.visible = false;
   }
   
   private function weatherErrorEventHandler(event:WeatherErrorEvent):void
   {
    Alert.show("지금 서버와 연결할 수 없거나\n올바른 검색어가 아닙니다.", "오류");
    bar.visible = false;
   }
  ]]>
 </mx:Script>
 
 <mx:DateFormatter id="dateFormatter" formatString="DD일 HH시NN분"/>
 
 <mx:Panel width="100%" height="100%" paddingTop="5" paddingRight="5"
   paddingBottom="5" paddingLeft="5" title="Weather Widget">
  <mx:VBox width="100%">
   <mx:HBox>
    <mx:Label text="지역"/>
    <mx:ComboBox id="location_cb"/>
    <mx:Button label="검색" buttonMode="true" click="doSubmit()"/>
    <mx:ProgressBar id="bar" labelPlacement="bottom" themeColor="#070D7A"
      indeterminate="true" minimum="0" maximum="100" visible="false"
       includeInLayout="true" label="now Loading..." direction="right"
        mode="manual" x="100" y="100" width="100%"/>
   </mx:HBox>
   <mx:HRule width="100%"/>
   
   <mx:HBox width="100%">
    <mx:VBox horizontalAlign="center" width="33%">
     <mx:Label id="fore0Date_lb"/>
     <mx:Image id="fore0_img"/>
     <mx:Label id="fore0Description_lb"/>
     <mx:HBox>
      <mx:Label text="기온(℃)"/>
      <mx:Label id="fore0Temperature_lb"/>
     </mx:HBox>
    </mx:VBox>
   
    <mx:VRule height="100%"/>
   
    <mx:VBox horizontalAlign="center" width="33%">
     <mx:Label id="currentDate_lb"/>
     <mx:Image id="current_img"/>
     <mx:Label id="currentDescription_lb"/>
     <mx:HBox>
      <mx:Label text="기온(℃)"/>
      <mx:Label id="currentTemperature_lb"/>
     </mx:HBox>
    </mx:VBox>
   
    <mx:VRule height="100%"/>
   
    <mx:VBox horizontalAlign="center" width="33%">
     <mx:Label id="fore1Date_lb"/>
     <mx:Image id="fore1_img"/>
     <mx:Label id="fore1Description_lb"/>
     <mx:HBox>
      <mx:Label text="기온(℃)"/>
      <mx:Label id="fore1Temperature_lb"/>
     </mx:HBox>
    </mx:VBox>
   </mx:HBox>
 
  </mx:VBox>
  <mx:ControlBar/>
 </mx:Panel>
 
</mx:Application>

=====================================================================================================

** Yahoo DEVELOPER NETWORK

'OM' 카테고리의 다른 글

에어 디스크립터 파일  (0) 2008.06.03
외부에서 load된 swf가 MouseEvent를 가릴때  (4) 2008.05.18
캐릭터 이동  (2) 2008.05.11
Yahoo WebAPI를 이용한 날씨 위젯 2  (0) 2008.05.11
Flash Javascript 연동 (ExternalInterface)  (0) 2008.05.07
애니메이션  (0) 2008.05.07
생명게임  (0) 2008.05.05
Card Match 2  (0) 2008.05.04
원 그리기 (MouseListener)  (0) 2008.05.02