Select to view content in your preferred language

locator task synchronous response

824
2
09-20-2010 01:38 PM
SebastianRoberts
Frequent Contributor
I have a widget that allows a user to upload a text file which is read into a data grid.  The I call the locator task "addresstoLocations" with an AsynchronousResponder.  In my onREsult function, I can access an Array with all the result candidates, but it seems that I don't have access to the actual address that was sent to the geocoder.  I would like to be able to tie the values in the data grid back to their corresponding graphics (so I can highlight or zoom to).  This doesn't seem possible with an asynchronous responder becasue I dont' know which return belongs to which datagrid element.  I'd like to be able to add the seach address to the graphic as an attribute, or add the matched address to the dataGrid in a second column.  Without one of those solutions, the only thing I can do when a user clicks on a datagrid row is match the data grid address string with the graphic address string which only works for exact matches that have a score of 100.
  Is there some way to access the address field that was sent to the task in the Result function?  Or alternatively, can you run this as a synchronous response, so that you still have access to all the variables in the calling function?


private function doFind():void    {
    myGraphicsLayer.clear();
    var myAddress:Object;
    var myOutFields:Array = ["Match_addr"]; 
    addressData = new ArrayCollection();
    
    for (var i:Number=0; i <  addressDataImported.length; i++) {
     var tempAddressObj:GeoAddress = new GeoAddress();
     tempAddressObj.address = addressDataImported.street;
     
     
     myAddress = {Street:tempAddressObj.address};   
     locateTask.addressToLocations(myAddress, myOutFields, new AsyncResponder(onResult, onFault));
    }
    
    //populates the data Grid with the actual addresses returned.
    addressDataImported = addressData;
    map.addLayer(myGraphicsLayer);
    
    
    
    //nested function
    function onResult(candidates:Array, token:Object = null):void   {
     if (candidates.length > 0)  {
      var addressCandidate:AddressCandidate = candidates[0];  
      var myGraphic:Graphic = new Graphic();                          
      myGraphic.geometry = addressCandidate.location;
      myGraphic.symbol = resultSymbol;
      myGraphic.toolTip = addressCandidate.address.toString();
      myGraphic.attributes = addressCandidate.address;
      addressData.addItem(addressCandidate.address);
      myGraphic.addEventListener( MouseEvent.MOUSE_OVER, onMouseOver );
      myGraphic.addEventListener( MouseEvent.MOUSE_OUT, onMouseOut );
      myGraphicsLayer.add(myGraphic);
     }
     
     function onFault(info:Object, token:Object = null):void    {
      Alert.show("Failure: \n" + info.toString());
      }
     
    }
    
    private function onItemClick(event: ListEvent):void
   {
    if (myGraphicsLayer.numGraphics > 0){             
     highlightedGraphic = findGraphicByAttribute(event.itemRenderer.data)
     if (highlightedGraphic) {
      var myPoint:MapPoint = highlightedGraphic.geometry as MapPoint;
      map.scale = 2257;
      map.centerAt(myPoint);
 
     }
    }
   }
   
   public function findGraphicByAttribute( attributes : Object ) : Graphic
   {
    for each( var graphic : Graphic in myGraphicsLayer.graphicProvider)
    {
                                                                //only can search for an exact match
     if ( graphic.attributes. === attributes)
     {
      return graphic;
     }
    }   
    return null;
   }
Tags (2)
0 Kudos
2 Replies
RobertScheitlin__GISP
MVP Emeritus
Sebastian,

Responders have the ability for you to sent some token object with them, so you could seent the original address with the request to addressToLocations.

locateTask.addressToLocations(myAddress, myOutFields, new AsyncResponder(onResult, onFault,"The original Address"));

//That's what the token is in the result 
function onResult(candidates:Array, token:Object = null):void
0 Kudos
SebastianRoberts
Frequent Contributor
Thanks Robert,
  That did the trick.  I was able to add the search address as an attribute to the graphics layer.  Now when someone rolls over a row in the data grid, I can loop through the graphics and search for corresponding search address in the graphics layer.
  Thanks so much for your help as always,
Sebastian Roberts
0 Kudos