<?xml version="1.0" encoding="utf-8"?>
<!--
    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
-->
<mx:Application 
    xmlns:mx="http://www.adobe.com/2006/mxml" 
    layout="absolute" 
    creationComplete="handleCreationComplete(event);" 
    backgroundGradientAlphas="[1.0, 1.0]" 
    backgroundGradientColors="[#FFFFFF, #FFFFFF]" 
    viewSourceURL="srcview/index.html">
    
    <mx:UIComponent id="mapContainer" width="100%" height="100%" />
    <mx:Panel width="311" height="143" layout="absolute" left="10" top="10" id="mapStatus" title="Map Status" borderColor="#A69F9F">
        <mx:TextInput text="Center LatLon: {_centerLatLon}" top="10" left="10" right="10"/>
        <mx:TextInput editable="false" text="Center Address: {_centerAddressStr}" left="10" top="36" right="10"/>
        <mx:Button click="_yahooMap.mapType=MapTypes.MAP" label="Map" width="85" horizontalCenter="-93" bottom="10"/>
        <mx:Button click="_yahooMap.mapType=MapTypes.SATELLITE" label="Satellite" width="85" horizontalCenter="0" bottom="10"/>
        <mx:Button click="_yahooMap.mapType=MapTypes.HYBRID" label="Hybrid" bottom="10" width="85" horizontalCenter="93"/>
    </mx:Panel>
    
    <mx:Script>
        <![CDATA[
            import mx.events.ResizeEvent;
            
            import com.yahoo.maps.api.MapTypes;
            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.widgets.WidgetAlign;
            
            import com.yahoo.maps.webservices.geocoder.GeocoderResultSet;
            import com.yahoo.maps.webservices.geocoder.GeocoderResult;
            import com.yahoo.maps.webservices.geocoder.events.GeocoderEvent;
            import com.yahoo.maps.webservices.geocoder.Geocoder;
            
            private var _yahooMap:YahooMap;
            private var _centerAddress:Address;
            private var _geocoder:Geocoder;
            
            [Bindable] private var _centerLatLon:LatLon;
            [Bindable] private var _centerAddressStr:String;
            
            private function handleCreationComplete(event:Event):void
            {
                // 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 = Application.application.parameters.appid;
                
                // create YahooMap instance and listen for map initialize event
                _yahooMap = new YahooMap(); 
                _yahooMap.addEventListener(YahooMapEvent.MAP_INITIALIZE, handleMapInitialize); 
                _yahooMap.init(appid, mapContainer.width, mapContainer.height);
                
                // add pan control
                _yahooMap.addPanControl();
                
                // add zoom widget
                _yahooMap.addZoomWidget();
                
                // set the zoom widget align to top right of map.
                _yahooMap.zoomWidget.align = WidgetAlign.TOP_RIGHT;
                
                // add _yahooMap to the display list as child of the mapContainer UIComponent
                mapContainer.addChild(_yahooMap);
            }
            
            private function handleMapInitialize(event:YahooMapEvent):void
            {
                // listen for the mapContainer UIComponent resize event.
                mapContainer.addEventListener(ResizeEvent.RESIZE, handleContainerResize);
                
                // create new Geocoder and listen for its GEOCODER_SUCCESS event.
                _geocoder = new Geocoder();
                _geocoder.addEventListener(GeocoderEvent.GEOCODER_SUCCESS, handleGeocodeSuccess);
                
                // create new Address object to set the initial center of the map.
                _centerAddress = new Address(this.loaderInfo.parameters.address || "San Francisco, CA");
                _centerAddress.addEventListener(GeocoderEvent.GEOCODER_SUCCESS, handleCenterGeocodeSuccess);
                _centerAddress.geocode();
            }
            
            private function handleGeocodeSuccess(event:GeocoderEvent):void
            {
                // get the GeocoderResultSet containing each result returned by the geocoder.
                var resultSet:GeocoderResultSet = event.data as GeocoderResultSet;
                
                // grab the first (highest-quality) result.
                var firstResult:GeocoderResult = resultSet.firstResult;
                
                // get the full address string of the result.
                _centerAddressStr = firstResult.getLineAddress();
            }
            
            private function handleCenterGeocodeSuccess(event:GeocoderEvent):void
            {
                // grab the first (highest-quality) result. 
                var result:GeocoderResult = _centerAddress.geocoderResultSet.firstResult;
                
                // set the zoom level and centerLatLon of the map based on the GeocoderResult
                _yahooMap.zoomLevel = result.zoomLevel;
                _centerLatLon = _yahooMap.centerLatLon = result.latlon;
                
                // get the full address string of the result. 
                _centerAddressStr = result.getLineAddress();
                
                // reverse geocode the center LatLon of the map.
                reverseGeocodeCenter();
                
                // add event listeners to the map for MAP_MOVE, MAP_DRAG_STOP and MAP_DOUBLE_CLICK
                _yahooMap.addEventListener(YahooMapEvent.MAP_MOVE, handleMapMove);
                _yahooMap.addEventListener(YahooMapEvent.MAP_DRAG_STOP, handleMapMoveStop);
                _yahooMap.addEventListener(YahooMapEvent.MAP_DOUBLE_CLICK, handleDoubleClick);
            }
            
            private function handleMapMove(event:YahooMapEvent):void
            {
                // set the centerLatLon property in the map status window.
                _centerLatLon = _yahooMap.centerLatLon;
            }
            
            private function handleMapMoveStop(event:YahooMapEvent):void
            {
                // set the centerLatLon property in the map status window.
                _centerLatLon = _yahooMap.centerLatLon;
                
                // reverse geocode the center LatLon of the map.
                reverseGeocodeCenter();
            }
            
            private function handleDoubleClick(event:YahooMapEvent):void
            {
                // zoom the map in by one level.
                _yahooMap.zoomLevel--;
                
                // set the centerLatLon property in the map status window.
                _centerLatLon = _yahooMap.centerLatLon;
                
                // reverse geocode the center LatLon of the map.
                reverseGeocodeCenter();
            }
            
            private function reverseGeocodeCenter():void
            {
                // call out to the Geocoder to reverse geocode the center LatLon of the map.
                _geocoder.reverseGeocode(_yahooMap.centerLatLon);
            }
            
            private function handleContainerResize(event:ResizeEvent):void
            {
                // set the width and height of the map based on the container UIComponent size.
                _yahooMap.setSize( mapContainer.width, mapContainer.height);
            }
        ]]>
    </mx:Script>
</mx:Application>