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;
}