Select to view content in your preferred language

Reverse Geocode a collection of MapPoints

943
1
07-06-2011 04:52 AM
RedaKechouri
Emerging Contributor
Hi all,

This might be quite obvious to some of you, bu I can't get my head around this problem : basically, I'm trying to reverse geocode a collection of points, but due to the Asynchronous nature of the Locator Task, I only get the last result populated...

Here is a bit of code, I must do something fundamentally wrong here :
private function getAddress(myPoints:ArrayCollection):void
{
 for each (var myPoint:Object in myPoints)
 {
  locateTask.addEventListener(LocatorEvent.LOCATION_TO_ADDRESS_COMPLETE, locateTask_locationToAddressCompleteHandler);
     
  locateTask.locationToAddress(myPoint.point, 100);
     
  function locateTask_locationToAddressCompleteHandler(event:LocatorEvent):void
  {
   var candidate:AddressCandidate = event.addressCandidate;
      
   if (candidate && candidate.address && candidate.address.Name)
   {
    var address:Object = candidate.address;
    myPoint.address = address.Name;
   }
   else
   {
    myPoint.address = "N/A";
   }
     
   locateTask.removeEventListener(LocatorEvent.LOCATION_TO_ADDRESS_COMPLETE, locateTask_locationToAddressCompleteHandler);
      
  }
 }
}


Many thanks for your help....
Tags (2)
0 Kudos
1 Reply
RobertScheitlin__GISP
MVP Emeritus
Reda,

   Try separating out the result function and not removing the listener:

private function getAddress(myPoints:ArrayCollection):void
            {
                for each (var myPoint:Object in myPoints)
                {
                    locateTask.addEventListener(LocatorEvent.LOCATION_TO_ADDRESS_COMPLETE, locateTask_locationToAddressCompleteHandler);
                    locateTask.locationToAddress(myPoint.point, 100);
                }
            }
            
            private function locateTask_locationToAddressCompleteHandler(event:LocatorEvent):void
            {
                var candidate:AddressCandidate = event.addressCandidate;
                if (candidate && candidate.address && candidate.address.Name){
                    var address:Object = candidate.address;
                    myPoint.address = address.Name;
                }else{
                    myPoint.address = "N/A";
                }
            }
0 Kudos