package {
    import com.adobe.viewsource.ViewSource;
    import com.yahoo.maps.api.YahooMap;
    import com.yahoo.maps.api.YahooMapEvent;
    import com.yahoo.maps.api.core.location.Address;
    import com.yahoo.maps.api.core.location.LatLon;
    import com.yahoo.maps.api.utils.Distance;
    import com.yahoo.maps.overlays.CircleOverlay;
    import com.yahoo.maps.webservices.geocoder.GeocoderResult;
    import com.yahoo.maps.webservices.geocoder.events.GeocoderEvent;
    
    import flash.display.Sprite;

    public class YahooMap_CirclePoly extends Sprite
    {
        private var _yahooMap:YahooMap;
        private var _centerAddress:Address;
        
        public function YahooMap_CirclePoly()
        {
            ViewSource.addMenuItem(this, "srcview/index.html", true);
            
            this.stage.align = "topLeft";
            this.stage.scaleMode = "noScale";
            
            // this examples uses an application id passed into the app via FlashVars.
            // Get your own from the Yahoo! Developer Network @ http://developer.yahoo.com/wsregapp/
            var appid:String = this.loaderInfo.parameters.appid;
            
            _yahooMap = new YahooMap();
            _yahooMap.addEventListener(YahooMapEvent.MAP_INITIALIZE, handleMapInitialize);
            _yahooMap.init(appid, this.stage.stageWidth, this.stage.stageHeight);
            
            this.addChild(_yahooMap);
        }
        
        private function handleMapInitialize(event:YahooMapEvent):void
        {
            _centerAddress = new Address(this.loaderInfo.parameters.address || "Seattle, WA");
            _centerAddress.addEventListener(GeocoderEvent.GEOCODER_SUCCESS, handleGeocodeSuccess);
            _centerAddress.geocode();
        }
        
        private function handleGeocodeSuccess(event:GeocoderEvent):void
        {
            var result:GeocoderResult = _centerAddress.geocoderResultSet.firstResult;
            
            _yahooMap.zoomLevel = result.zoomLevel;
            _yahooMap.centerLatLon = result.latlon;
            
            _yahooMap.addPanControl();
            _yahooMap.addScaleBar();
            _yahooMap.addZoomWidget();
            
            _yahooMap.addEventListener(YahooMapEvent.MAP_CLICK, handleMapClick);
            
            drawCircle(_yahooMap.centerLatLon);
        }
        
        private function handleMapClick(event:YahooMapEvent):void
        {
            drawCircle(event.data.latlon);            
        }
        
        private function drawCircle(latlon:LatLon):void
        {
            var overlay:CircleOverlay = new CircleOverlay(latlon, Distance.milesToMeters(2));
            
            _yahooMap.overlayManager.addOverlay(overlay);
            
            overlay.fillColor = 0xFF0000;
            overlay.fillAlpha = 0.5;
            overlay.lineColor = 0x000080;
            overlay.lineAlpha = 0.75;
            overlay.lineThickness = 2;
            
            overlay.redraw();
        }
    }
}