본문 바로가기

OM

Yahoo WebAPI를 이용한 날씨 위젯 2



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

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
  width="250" height="450" creationComplete="init()">
 <mx:Style>
  Application{
   backgroundColor : #ffffff;
   fontSize : 12;
  }
 </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:"KSXX0011"},
     {label:"광주", code:"KSXX0014"},
     {label:"대구", code:"KSXX0026"},
     {label:"대전", code:"KSXX0027"},
     {label:"부산", code:"KSXX0050"},
     {label:"울릉도", code:"KSXX0039"},
     {label:"인천", code:"KSXX0009"},
     {label:"울산", code:"KSXX0029"},
     {label:"전주", code:"KSXX0047"},
     {label:"제주", code:"KSXX0004"},
     {label:"청주", code:"KSXX0007"},
     {label:"춘천", code:"KSXX0035"},
    ];
   
   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;
   
    date_lb.text = dateFormatter.format(weather.date);
    description_img.source = current.imageURL;
    description_lb.text = current.description;
    temperature_lb.text = current.temperature + " ℃";
    chill_lb.text = current.wind.chill + " ℃";
    windSpeed_lb.text = current.wind.speed + " k/h";
    humidity_lb.text = current.atmosphere.humidity + " %";
    sunrise_lb.text = dateFormatter.format(current.astronomy.sunrise);
    sunset_lb.text = dateFormatter.format(current.astronomy.sunset);
   
    bar.visible = false;
   }
   
   private function weatherErrorEventHandler(event:WeatherErrorEvent):void
   {
    Alert.show("지금 서버와 연결할 수 없거나\n올바른 검색어가 아닙니다.", "오류");
    bar.visible = false;
   }
  ]]>
 </mx:Script>
 
 <mx:DateFormatter id="dateFormatter" formatString="YYYY년MM월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:HBox>
   <mx:HRule width="100%"/>
   <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:VBox>
 
  <mx:VBox width="100%">
   <mx:HBox>
    <mx:Label id="date_lb"/>
   </mx:HBox>
   <mx:HBox>
    <mx:Image id="description_img"/>
   </mx:HBox>
   <mx:HBox>
    <mx:Label text="오늘의 날씨"/>
    <mx:Label id="description_lb" fontWeight="bold"/>
   </mx:HBox>
   <mx:HBox>
    <mx:Label text="온도"/>
    <mx:Label id="temperature_lb"/>
   </mx:HBox>
   <mx:HBox>
    <mx:Label text="체감온도"/>
    <mx:Label id="chill_lb"/>
   </mx:HBox>
   <mx:HBox>
    <mx:Label text="풍속"/>
    <mx:Label id="windSpeed_lb"/>
   </mx:HBox>
   <mx:HBox>
    <mx:Label text="습도"/>
    <mx:Label id="humidity_lb"/>
   </mx:HBox>
   <mx:HBox>
    <mx:Label text="일출"/>
    <mx:Label id="sunrise_lb"/>
   </mx:HBox>
   <mx:HBox>
    <mx:Label text="일몰"/>
    <mx:Label id="sunset_lb"/>
   </mx:HBox>
  </mx:VBox>
 
  <mx:ControlBar horizontalAlign="right">
   <mx:LinkButton label="Created by Shallaa" fontSize="10"
     click="navigateToURL(new URLRequest('http://shallaazm.tistory.com'));"
     textDecoration="underline" alpha="0.20"/>
  </mx:ControlBar>
 </mx:Panel>
</mx:Application>

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

** Yahoo DEVELOPER NETWORK

'OM' 카테고리의 다른 글

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