<?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" 
    backgroundColor="#FFFFFF" 
    creationComplete="handleCreationComplete()" 
    viewSourceURL="srcview/index.html">
    
    <mx:UIComponent id="mapContainer" width="100%" bottom="0" top="35"/>
    <mx:Canvas id="header" width="100%" height="35" backgroundColor="#E5E5E5" backgroundAlpha="0.85">
        <mx:TextInput id="searchInput" width="275" verticalCenter="0" left="15" enter="getTextInput();"/>
        <mx:Button label="Search" cornerRadius="1" verticalCenter="0" left="288" click="getTextInput()"/>
    </mx:Canvas>
    <mx:Panel width="343" height="200" layout="absolute" id="gResultPanel" title="Results" left="35" top="45">
        <mx:List left="0" right="0" top="0" bottom="0" id="geocoderResultsList" dataProvider="{_geocoderResults}" change="handleListChange();"/>
    </mx:Panel>
    
    <mx:Script>
        <![CDATA[
            import com.yahoo.maps.api.intl.MapLocales;
            import mx.collections.ArrayCollection;
            import mx.events.ResizeEvent;
            import com.yahoo.maps.webservices.geocoder.GeocoderResult;
            import com.yahoo.maps.webservices.geocoder.GeocoderResultSet;
            import com.yahoo.maps.webservices.geocoder.events.GeocoderEvent;
            import com.yahoo.maps.api.core.location.Address;
            import com.yahoo.maps.api.core.location.LatLon;
            import com.yahoo.maps.api.YahooMapEvent;
            import com.yahoo.maps.api.YahooMap;
            
            private var _yahooMap:YahooMap;
            private var _address:Address;
        
            [Bindable] private var _geocoderResults:ArrayCollection;
        
            private function handleCreationComplete():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);
                
                _yahooMap.addPanControl();
                _yahooMap.addScaleBar();
                _yahooMap.addTypeWidget();
                _yahooMap.addZoomWidget();
                
                mapContainer.addChild(_yahooMap);
                
                _geocoderResults = new ArrayCollection();
                gResultPanel.visible=false;
            }
            
            private function handleMapInitialize(event:YahooMapEvent):void 
            {
                mapContainer.addEventListener(ResizeEvent.RESIZE, handleContainerResize);
                
                _yahooMap.zoomLevel = 13;
                _yahooMap.centerLatLon = new LatLon(40.81,-96.7);
            }
            
            private function handleContainerResize(event:ResizeEvent):void 
            {
                _yahooMap.setSize( mapContainer.width, mapContainer.height );
            }
            
            private function getTextInput():void 
            {
                var searchStr:String = searchInput.text;
                
                searchInput.text = "Looking up address...";
                
                _address = new Address(searchStr);
                _address.addEventListener(GeocoderEvent.GEOCODER_SUCCESS, handleGeocodeSuccess);
                _address.geocode();
            }
            
            private function handleGeocodeSuccess(event:GeocoderEvent):void 
            {
                var geocoderResults:GeocoderResultSet = _address.geocoderResultSet;
                var results:Array = geocoderResults.results;
                
                // if we only have one result, set the location right away.
                if(geocoderResults.found == 1) 
                {
                    setLocation(geocoderResults.firstResult);
                }
                else if(geocoderResults.found > 1) 
                {
                    // loop through each result and add an item to the suggest list.
                    _geocoderResults = new ArrayCollection();
                    var len:int = results.length;
                    for(var i:int=0; i<len; i++) 
                    {
                        var result:GeocoderResult = results[i];
                        
                        var data:Object = {
                            label: result.getLineAddress(),
                            data:result
                        }
                        
                        _geocoderResults.addItem(data);
                    }
                    
                    //show the suggestions panel
                    gResultPanel.visible=true;
                }else{
                    // reset the text
                    searchInput.text = "";
                }
            }
            
            private function setLocation(geocoderResult:GeocoderResult):void
            {
                // set the zoom level, center latlon and set the text field with the full address.
                _yahooMap.zoomLevel = geocoderResult.zoomLevel;
                _yahooMap.centerLatLon = geocoderResult.latlon;
                searchInput.text = geocoderResult.getLineAddress();
            }
            
            private function handleListChange():void 
            {
                // get the selected geocoder result object
                var selectedResult:GeocoderResult = geocoderResultsList.selectedItem.data as GeocoderResult;
                setLocation(selectedResult);
                
                _geocoderResults.removeAll();
                gResultPanel.visible=false;
            }
        ]]>
    </mx:Script>
</mx:Application>