Solved! Go to Solution.
private const ADDRESS_FROM_LOCATION:uint = 0x1; private const ADDRESS_TO_LOCATION: uint = 0x2; private function findNearestAddress(location:MapPoint, distance:Number):void { var token:Object = { type: ADDRESS_FROM_LOCATION }; locateTask.locationToAddress(location, distance, new AsyncResponder(onLocateTaskResult, onLocateTaskFault, token)); } private function executeLocatorTask(address:String):void { // search attributes { Attribute1: "attribute value", Attribute2: "attribute value"} // defined in/by geocode service as request parameters var token:Object = { type: ADDRESS_TO_LOCATION }; var addressObj:Object = { StreetName: address }; var outFields:Array = [ "*" ]; locateTask.outSpatialReference = map.spatialReference; locateTask.addressToLocations(addressObj, outFields, new AsyncResponder(onLocateTaskResult, onLocateTaskFault, token)); } protected function onLocateTaskResult(result:*, token:Object = null):void { var candidates:Array = null; var addressCandidate:AddressCandidate = null; if (result is Array) { candidates = result as Array; } else if (result is AddressCandidate) { addressCandidate = result as AddressCandidate; } if (token != null && token.type != null) { switch (token.type) { case ADDRESS_TO_LOCATION: { var bestCandidates:Array = findBestScoredCandidates(candidates); var suitableCandidates:Array = findSuitableCandidates(candidates, 80); break; } case ADDRESS_FROM_LOCATION: { showCandidateOnMap(addressCandidate); break; } } } } protected function onLocateTaskFault(info:Object, token:Object = null):void { trace(StringUtil.substitute("Error\n>>>\n{0}", info.toString())); } private function showCandidateOnMap(candidate:AddressCandidate):void { myGraphicsLayer.clear(); // var address:Object = candidate.address; var candidateMapPoint:MapPoint = candidate.location; if (candidateMapPoint.spatialReference.wkid != myMap.spatialReference.wkid) { candidateMapPoint = WebMercatorUtil.geographicToWebMercator(candidate.location) as MapPoint; } var myGraphic:Graphic = new Graphic(candidateMapPoint, mySymbol, address); myGraphic.toolTip = address.Address.toString(); myGraphicsLayer.add(myGraphic); } private function findBestScoredCandidates(candidates:Array):Array { if (candidates != null) { if (candidates.length > 0) { var bestScore:Number = 0; var bestScoredCandidates:Array = new Array(); for (var index:int; index < candidates.length; index++) { var nextCandidate:AddressCandidate = candidates[index]; if (nextCandidate.score > bestScore) // candidate with better score found { bestScoredCandidates = new Array(); bestScoredCandidates.push(nextCandidate); bestScore = nextCandidate.score; } else if (nextCandidate.score > bestScore) // candidate with same score found { bestScoredCandidates.push(nextCandidate); } } return bestScoredCandidates; } } return null; } private function findSuitableCandidates(candidates:Array, suitableScore:Number):Array { if (candidates != null) { if (candidates.length > 0) { var suiatableCandidates:Array = new Array(); for (var index:int; index < candidates.length; index++) { var nextCandidate:AddressCandidate = candidates[index]; if (nextCandidate.score >= suitableScore) { suiatableCandidates.push(nextCandidate); } } return suiatableCandidates; } } return null; }
private const ADDRESS_FROM_LOCATION:uint = 0x1; private const ADDRESS_TO_LOCATION: uint = 0x2; private function findNearestAddress(location:MapPoint, distance:Number):void { var token:Object = { type: ADDRESS_FROM_LOCATION }; locateTask.locationToAddress(location, distance, new AsyncResponder(onLocateTaskResult, onLocateTaskFault, token)); } private function executeLocatorTask(address:String):void { // search attributes { Attribute1: "attribute value", Attribute2: "attribute value"} // defined in/by geocode service as request parameters var token:Object = { type: ADDRESS_TO_LOCATION }; var addressObj:Object = { StreetName: address }; var outFields:Array = [ "*" ]; locateTask.outSpatialReference = map.spatialReference; locateTask.addressToLocations(addressObj, outFields, new AsyncResponder(onLocateTaskResult, onLocateTaskFault, token)); } protected function onLocateTaskResult(result:*, token:Object = null):void { var candidates:Array = null; var addressCandidate:AddressCandidate = null; if (result is Array) { candidates = result as Array; } else if (result is AddressCandidate) { addressCandidate = result as AddressCandidate; } if (token != null && token.type != null) { switch (token.type) { case ADDRESS_TO_LOCATION: { var bestCandidates:Array = findBestScoredCandidates(candidates); var suitableCandidates:Array = findSuitableCandidates(candidates, 80); break; } case ADDRESS_FROM_LOCATION: { showCandidateOnMap(addressCandidate); break; } } } } protected function onLocateTaskFault(info:Object, token:Object = null):void { trace(StringUtil.substitute("Error\n>>>\n{0}", info.toString())); } private function showCandidateOnMap(candidate:AddressCandidate):void { myGraphicsLayer.clear(); // var address:Object = candidate.address; var candidateMapPoint:MapPoint = candidate.location; if (candidateMapPoint.spatialReference.wkid != myMap.spatialReference.wkid) { candidateMapPoint = WebMercatorUtil.geographicToWebMercator(candidate.location) as MapPoint; } var myGraphic:Graphic = new Graphic(candidateMapPoint, mySymbol, address); myGraphic.toolTip = address.Address.toString(); myGraphicsLayer.add(myGraphic); } private function findBestScoredCandidates(candidates:Array):Array { if (candidates != null) { if (candidates.length > 0) { var bestScore:Number = 0; var bestScoredCandidates:Array = new Array(); for (var index:int; index < candidates.length; index++) { var nextCandidate:AddressCandidate = candidates[index]; if (nextCandidate.score > bestScore) // candidate with better score found { bestScoredCandidates = new Array(); bestScoredCandidates.push(nextCandidate); bestScore = nextCandidate.score; } else if (nextCandidate.score > bestScore) // candidate with same score found { bestScoredCandidates.push(nextCandidate); } } return bestScoredCandidates; } } return null; } private function findSuitableCandidates(candidates:Array, suitableScore:Number):Array { if (candidates != null) { if (candidates.length > 0) { var suiatableCandidates:Array = new Array(); for (var index:int; index < candidates.length; index++) { var nextCandidate:AddressCandidate = candidates[index]; if (nextCandidate.score >= suitableScore) { suiatableCandidates.push(nextCandidate); } } return suiatableCandidates; } } return null; }