<?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()" 
    viewSourceURL="srcview/index.html">
    
    <mx:Label text="Address 1:" horizontalCenter="-141" top="91"/>
    <mx:TextInput id="startInput" width="200" horizontalCenter="0" top="89"/>

    <mx:Label text="Address 2:" horizontalCenter="-141" top="121"/>
    <mx:TextInput id="endInput" width="200" horizontalCenter="0" top="119"/>
    
    <mx:Button label="Get Distance" horizontalCenter="50" top="149" click="getAddrDistance();"/>
    <mx:Label text="Total Distance: {_totalDistance} miles" fontSize="14" horizontalCenter="0" top="225"/>
    <mx:Script>
        <![CDATA[
            import com.yahoo.maps.api.config.MapConfig;
            import com.yahoo.maps.api.core.location.Address;
            import com.yahoo.maps.api.utils.Distance;
            import com.yahoo.maps.api.utils.DistanceResult;
            import com.yahoo.maps.webservices.geocoder.events.GeocoderEvent;
            import com.yahoo.maps.webservices.geocoder.Geocoder;
            import com.yahoo.maps.webservices.geocoder.GeocoderResult;
            
            [Bindable] private var _totalDistance:String = "0";
            
            private var _geocoderResults:Array;
            private var _addr1:Address;
            private var _addr2:Address;
            
            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;
                
                // pass your appid into the MapConfig object to initialize the API.
                var config:MapConfig = MapConfig.getInstance(appid);
            }
            
            private function getAddrDistance():void 
            {
                if(startInput.text != "" && endInput.text != "")  
                {
                    _geocoderResults = new Array();
                    
                    // create two address objects to geocode the user input
                    
                    _addr1 = new Address(startInput.text);
                    _addr1.addEventListener(GeocoderEvent.GEOCODER_FAILURE, handleGeoFail);
                    _addr1.addEventListener(GeocoderEvent.GEOCODER_SUCCESS, handleGeoSuccess);
                    _addr1.geocode();
                    
                    _addr2 = new Address(endInput.text);
                    _addr2.addEventListener(GeocoderEvent.GEOCODER_FAILURE, handleGeoFail);
                    _addr2.addEventListener(GeocoderEvent.GEOCODER_SUCCESS, handleGeoSuccess);
                    _addr2.geocode();
                }
            }
            
            private function handleGeoFail(event:GeocoderEvent):void 
            {
                // ...
            }
            
            
            private function handleGeoSuccess(event:GeocoderEvent):void 
            {
                var address:Address = event.target as Address;
                
                // grab the geocoded firstResult (highest quality match)  
                var firstResult:GeocoderResult = address.geocoderResultSet.firstResult;
                _geocoderResults.push(firstResult);
                
                // if we have both of the results returned...
                if(_geocoderResults.length==2) 
                {
                    // calculate the distance between the two locations and grab the distance in miles.
                    var miles:Number = Distance.getDistance( _geocoderResults[0].latlon, _geocoderResults[1].latlon ).miles;
                    
                    // set the _totalDistance (bindable) property with a precise miles value.
                    _totalDistance = miles.toPrecision(4);
                }    
            }
            
        ]]>
    </mx:Script>
</mx:Application>