<?xml version="1.0" encoding="utf-8"?>
<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
{
var appid:String = Application.application.parameters.appid;
var config:MapConfig = MapConfig.getInstance(appid);
}
private function getAddrDistance():void
{
if(startInput.text != "" && endInput.text != "")
{
_geocoderResults = new Array();
_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;
var firstResult:GeocoderResult = address.geocoderResultSet.firstResult;
_geocoderResults.push(firstResult);
if(_geocoderResults.length==2)
{
var miles:Number = Distance.getDistance( _geocoderResults[0].latlon, _geocoderResults[1].latlon ).miles;
_totalDistance = miles.toPrecision(4);
}
}
]]>
</mx:Script>
</mx:Application>