Select to view content in your preferred language

Identify result not showing up on programmatically added graphic layer

903
2
Jump to solution
08-06-2010 01:08 PM
KenBuja
MVP Esteemed Contributor
I'm running into a problem when attempting to see the graphicprovider for a graphic layer to show the results of an IdentifyTask in my program. I'm adding the graphic layer (along with a bunch of other layers) in the initialize code for the page.

I tried using the Identify sample from the Resource page (version 1.3) to run a test. When I click on a polygon with the original sample code, the polygon gets shaded with the symbology as expected. However, when I programmatically add in the graphic layer holding the lastIdentifyResultGraphic, the polygon does not get shaded as expected.

Where am I going wrong in this code?

<?xml version="1.0" encoding="utf-8"?>
<mx:Application
    xmlns:mx="http://www.adobe.com/2006/mxml"
    xmlns:esri="http://www.esri.com/2008/ags"
    layout="vertical"
    styleName="plain"
    pageTitle="Identify Features on the Map" initialize="init();">
<!--
    This sample shows how to identify features with a MapClick and the Identify task.

    The IdentifyParameters designate which layers are being identified.
    Identify operations can potentially return a lot of information
    depending on the number of layers being identified and a given tolerance.
    The tolerance is the number of pixels a feature is allowed to lie away
    from the clicked point in order to be counted as a result.

    In this sample, when user clicks the map, an "Identify" task is executed.

    When the task finishes executing, the identifyCompleteHandler function loops
    through the features in the IdentifyResult and adds them to the map.
--> 
    <mx:Script>
        <![CDATA[
            import com.esri.ags.Graphic;
            import com.esri.ags.events.MapMouseEvent;
            import com.esri.ags.geometry.Geometry;
            import com.esri.ags.symbol.InfoSymbol;
            import com.esri.ags.tasks.IdentifyParameters;
            import com.esri.ags.tasks.IdentifyResult;

            import mx.controls.Alert;
            import mx.rpc.AsyncResponder;
            
            [Bindable]
            private var lastIdentifyResultGraphic:Graphic;
            private var layerGraphics:GraphicsLayer = new GraphicsLayer;
            
            private function init():void
            {
                layerGraphics.graphicProvider = lastIdentifyResultGraphic;
                myMap.addLayer(layerGraphics);
            }
            
            private function mapClickHandler(event:MapMouseEvent):void
            {
                clickGraphicsLayer.clear();

                var identifyParams:IdentifyParameters = new IdentifyParameters();
                identifyParams.returnGeometry = true;
                identifyParams.layerIds = new Array([2]);
                identifyParams.tolerance = 3;
                identifyParams.width = myMap.width;
                identifyParams.height = myMap.height;
                identifyParams.geometry = event.mapPoint;
                identifyParams.mapExtent = myMap.extent;
                identifyParams.spatialReference = myMap.spatialReference;
               
                var clickGraphic:Graphic = new Graphic(event.mapPoint, clickPtSym);
                identifyTask.execute(identifyParams, new AsyncResponder(myResultFunction, myFaultFunction, clickGraphic));
                clickGraphicsLayer.add(clickGraphic);
            }

            private function myResultFunction(results:Array, clickGraphic:Graphic = null):void
            {
                if (results && results.length > 0)
                {
                    var result:IdentifyResult = results[0];
                    var resultGraphic:Graphic = result.feature;
                    switch (resultGraphic.geometry.type)
                    {
                        case Geometry.MAPPOINT:
                        {
                            resultGraphic.symbol = smsIdentify;
                            break;
                        }
                        case Geometry.POLYLINE:
                        {
                            resultGraphic.symbol = slsIdentify;
                            break;
                        }
                        case Geometry.POLYGON:
                        {
                            resultGraphic.symbol = sfsIdentify;
                            break;
                        }
                    }
                    lastIdentifyResultGraphic = resultGraphic;
                    
                    // update clickGraphic (from mouse click to returned feature)
                    clickGraphic.symbol = new InfoSymbol(); // use default renderer
                    clickGraphic.attributes = resultGraphic.attributes;
                }
            }
            
            private function myFaultFunction(error:Object, clickGraphic:Graphic = null):void
            {
                Alert.show(String(error), "Identify Error");
            }
        ]]>
    </mx:Script>

    <!-- start declarations -->
    
        <!-- Symbol for where the user clicked -->
        <esri:SimpleMarkerSymbol id="clickPtSym" style="x" color="0xFF0000" size="12"/>
        
        <!-- Symbol for Identify Result as Polyline -->
        <esri:SimpleLineSymbol id="slsIdentify" style="solid" color="0x00FF00" width="2" alpha="1"/>
    
        <!-- Symbol for Identify Result as Point -->
        <esri:SimpleMarkerSymbol id="smsIdentify" style="diamond" color="0x00FF00" size="15"/>
    
        <!-- Symbol for Identify Result as Polygon -->
        <esri:SimpleFillSymbol id="sfsIdentify"/>
    
        <!-- Identify Task -->
        <esri:IdentifyTask id="identifyTask"
            concurrency="last"
            url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer"/>
            
    <!-- end declarations -->

    <esri:Map id="myMap" mapClick="mapClickHandler(event)">
        <esri:extent>
            <esri:Extent xmin="-120" ymin="30" xmax="-100" ymax="50">
                <esri:SpatialReference wkid="4326"/>
            </esri:Extent>
        </esri:extent>
        <esri:ArcGISDynamicMapServiceLayer
            url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
        <!--<esri:GraphicsLayer graphicProvider="{lastIdentifyResultGraphic}"/>-->
        <esri:GraphicsLayer id="clickGraphicsLayer"/>
    </esri:Map>
    
</mx:Application>

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
DasaPaddock
Esri Regular Contributor
The graphicProvider is set in the init using:
layerGraphics.graphicProvider = lastIdentifyResultGraphic;

In the myResultFunction you're setting the lastIdentifyResultGraphic var to the resultGraphic. layerGraphics.graphicProvider isn't bound to the lastIdentifyResultGraphic so it's not being reset.

Try setting it like this in the result function:
layerGraphics.graphicProvider = resultGraphic;

View solution in original post

0 Kudos
2 Replies
DasaPaddock
Esri Regular Contributor
The graphicProvider is set in the init using:
layerGraphics.graphicProvider = lastIdentifyResultGraphic;

In the myResultFunction you're setting the lastIdentifyResultGraphic var to the resultGraphic. layerGraphics.graphicProvider isn't bound to the lastIdentifyResultGraphic so it's not being reset.

Try setting it like this in the result function:
layerGraphics.graphicProvider = resultGraphic;
0 Kudos
KenBuja
MVP Esteemed Contributor
Thanks for clearing that up, Dasa.
0 Kudos