Superclass:Sprite
package
{
import com.yahoo.webapis.weather.WeatherService;
import com.yahoo.webapis.weather.events.WeatherResultEvent;
import flash.display.Sprite;
[SWF(width="550", height="400",frameRate="36", background="0X23EEEE")]
public class YahooTest extends Sprite
{
public function YahooTest()
{
super();
//1.객체생성
var weather:WeatherService=new WeatherService
//2.데이터가 도착하면 할일
weather.addEventListener(WeatherResultEvent.WEATHER_LOADED, onComplete);
//3.데이터 호출
weather.getWeather("KSXX0037","c");
}
private function onComplete(e:WeatherResultEvent):void
{
trace("습도,온도,가시거리,풍향,풍속...모두 꺼낼 수 있다.");
trace(e.data)
//data는 오브젝트이기 때문에 dot을 찍었을때 세부네용이 모두 나오지 않았다. 다시말하면 data는 오브젝트로서 모든 정보가
뭉쳐져있는 상태인 것이다. 세부 테이터를 선택할 수 있도록 하려면 com > event 폴더에 저장 되어있는
WeatherResultEvent 파일에 getter를 첨부해주어야 한다. (자세한 코드는 아래에 첨부함)
//찾아들어가는 방식은 컨트롤을 누른 상태에서 "WeatherResultEvent"를 클릭하여 해당 이벤트로 바로 이동하거나 수동으로
찾아들어간다.
trace("온도:",e.weather.current.temperature); // 현재 온도를 찾아들어가는 과정 dot을 찍었을때 나오는 하위 정보를 선택
trace("풍속:",e.weather.current.wind.speed+"km/s");
trace("습도:",e.weather.current.atmosphere.humidity+"%");
trace("풍속:",e.weather.current.atmosphere.pressure+"mb");
//모든 정보를 때마다 외워서 코딩하는 것은 비 효율적이므로 위와 같이 간단하게 상식선에서 활용할 수 있도록 하는 것이 목적이다.
}
}//class
}//package
src>com>event>WeatherResultEvent
/*
Copyright (c) 2008 Yahoo! Inc. All rights reserved.
The copyrights embodied in the content of this file are licensed under the BSD (revised) open source license
*/
package com.yahoo.webapis.weather.events
{
import com.yahoo.webapis.weather.Weather;
import flash.events.Event;
/**
* Event class in response to data events from the Yahoo! Weather API.
*
* @langversion ActionScript 3.0
* @playerversion Flash 9
* @author Allen Rabinovich 02/10/07
*/
public class WeatherResultEvent extends Event
{
/**
* True if the event is the result of a successful call,
* False if the call failed
*/
public var success:Boolean;
private var _data:Object;
/**
* The type of event dispatched when Weather data has loaded successfully.
*/
public static const WEATHER_LOADED:String = "weatherLoaded";
/**
* Constructs a new WeatherResultEvent
*/
public function WeatherResultEvent(type:String, inData:Object)
{
_data = inData;
super( type, bubbles, cancelable );
}
//날씨 정보를 가져오는 getter
public function get weather():Weather
{
return _data as Weather; //_ 라는 것이 weather 를 뜻함
}
/**
* Data
*/
public function get data():Object
{
return _data;
}
public function set data( value:Object ):void
{
_data = value;
}
}
}
온도: 3
풍속: 3.22km/s
습도: 55%
풍속: 1015mb
[SWF] Users:hyemisong:Documents:Flex Builder 3:ex0210:bin-debug:YahooTest.swf - 13,277 bytes after decompression