IdentifyTask repeats itself based on number of times the map has been clicked...

633
2
Jump to solution
07-12-2012 01:04 PM
MattGiles
New Contributor
I have a Dynamic layer in my map that i am performing an IdentifyTask on. I have a function that, when the map is clicked, performs the identify using the geometry from the MapMouseEvent. I am trying to pull the name of the feature that was clicked (returned in IdentifyEvent). I have it working, however the identifyResult function (called myResultFunction) will repeat itself based on the number of times the map has been previously clicked (The first time clicking the map, the function runs once. The second time clicking the map, the function runs twice. ETC). Can someone explain this behavior?

I put a number of "test" alerts into the code to try and figure out whats going on. When the function runs twice, the order of the test alerts is as follows (from 1st (bottom alert) to last (top alert)): mouseClicked,1,2,3,4,5,1,2,3,4,5,6,NAME PRINTS,6,NAMEPRINTS.

private function mapClickHandler(event:MapMouseEvent):void{
    clickGraphicsLayer.clear();

                                Alert.show("mouseClicked");
    
    var identifyParams:IdentifyParameters = new IdentifyParameters();
     identifyParams.returnGeometry = true;
     identifyParams.tolerance = 5;
     identifyParams.width = map.width;
     identifyParams.height = map.height;
     identifyParams.geometry = event.mapPoint;
     identifyParams.mapExtent = map.extent;
     identifyParams.spatialReference = map.spatialReference;
    
    var clickGraphic:Graphic = new Graphic(event.mapPoint, clickPtSym);
    clickGraphicsLayer.add(clickGraphic);
    identifyTask.addEventListener(IdentifyEvent.EXECUTE_COMPLETE, myResultFunction);
    identifyTask.execute(identifyParams);
    
    
        //why does this entire function repeat itself based on the number of times the map has been clicked??????????????????????????
        function myResultFunction(event:IdentifyEvent):void
        {
         Alert.show("test1");
         
         var results:Array = event.identifyResults;
         
         if (results && results.length > 0)
         {
          Alert.show("test2");
          
          var result:IdentifyResult = results[0];
          var resultGraphic:Graphic = result.feature;
          var resultObj:Object = results[0].feature.attributes;
          // Alert.show(resultGraphic.attributes.toString());
          
          switch (resultGraphic.geometry.type){
           case Geometry.MAPPOINT:
           {
            Alert.show("test3");
            
            resultGraphic.symbol = selectSymbol;
            clickGraphicsLayer.add(resultGraphic);
            break;
           }
           case Geometry.POLYLINE:
           {
            //resultGraphic.symbol = slsIdentify;
            break;
           }
           case Geometry.POLYGON:
           {
            //resultGraphic.symbol = sfsIdentify;
            break;
           }
          }
          
          Alert.show("test4");
          
          lastIdentifyResultGraphic = resultGraphic;
          
          //switch to the proper table..
          var layerId:Number = result.layerId;
          var vbox:VBox = VBox(tn.getChildByName(myDynamicService.layerInfos[layerId].name+"_tab"));
          
          tn.selectedChild = vbox;
                   
          //switch to proper row in table..
          var datagrid:mx.controls.DataGrid = mx.controls.DataGrid(vbox.getChildByName(myDynamicService.layerInfos[layerId].name+"_dg"))
          var attributes:ArrayCollection = datagrid.dataProvider as ArrayCollection;
          
          
          
          var layer:FeatureLayer = new FeatureLayer(myDynamicService.url+"/"+layerId.toString());
           layer.addEventListener(LayerEvent.LOAD, layerLoad_Handler);
          
           Alert.show("test5");
           
           function layerLoad_Handler(event:LayerEvent):void{
            layer.removeEventListener(LayerEvent.LOAD, layerLoad_Handler);
            
            Alert.show("test6");
            
            var objID:String = String(results[0].feature.attributes["Name"]);
            Alert.show(objID);
            
            
            //var str:String = attributes.getItemAt(0)[layer.layerDetails.objectIdField].toString();
            //Alert.show(str);
           }
          
         }
        }
        
        function myFaultFunction(error:Object, clickGraphic:Graphic = null):void
        {
         Alert.show(String(error), "Identify Error");
        }
   }
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor
I think what's happening is that you're adding a listener event every time you click the map but not removing them, so you've got more and more listeners responding to each click.

View solution in original post

0 Kudos
2 Replies
KenBuja
MVP Esteemed Contributor
I think what's happening is that you're adding a listener event every time you click the map but not removing them, so you've got more and more listeners responding to each click.
0 Kudos
AndrewLiles
New Contributor
I'd recommend using an AsyncResponder instead of using .addEventListener each time:

identifyTask.execute(identifyParams, new AsyncResponder(identResult, identFault));

function identResult(result:IdentifyResult, token:Object = null):void
{
...
}

function identFault(fault:Fault, token:Object = null):void
{
...
}
0 Kudos