Select to view content in your preferred language

Need help with retrieving data values from Identify Task

535
5
01-17-2012 02:26 PM
BobNichols
Regular Contributor
I was wondering if someone could help me break the down the Identity Task.  I have been looking at the interactive example of Identity task as well as the the example located in the concepts area.  Neither of them really explain the process of the task itself.  I have been trying, with very little luck, to use the identity task to be able to click on a facility on a map and return the facility id (FAC_ID) and display it in a textbox.  The only thing that I have been able to get it to do is to return the facility's Short Name, which is a field associated with the map service.  I am having major issues with trying to define and assign the facility ID field to the text property of a text box. Can anyone one type up a quick example of how to do this.  I have to imagine that it is extremely easy to do but I am just getting started and still trying to wrap my head around everything.   

private void MyMap_MouseClick(object sender, Map.MouseEventArgs args)
       {
           // Show an icon at the Identify location.
           GraphicsLayer graphicsLayer = MyMap.Layers["IdentifyIconGraphicsLayer"] as GraphicsLayer;
           graphicsLayer.ClearGraphics();
           ESRI.ArcGIS.Client.Graphic graphic = new ESRI.ArcGIS.Client.Graphic()
           {
               Geometry = args.MapPoint,
               Symbol = LayoutRoot.Resources["IdentifyLocationSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol
           };
          
           graphicsLayer.Graphics.Add(graphic);

           // Identify task initialization.
           IdentifyTask identifyTask = new IdentifyTask("URL to my REST directory map server");
           identifyTask.ExecuteCompleted += IdentifyTask_ExecuteCompleted;
           identifyTask.Failed += IdentifyTask_Failed;

           // Initialize Identify parameters. Specify search of all layers.
           IdentifyParameters identifyParameters = new IdentifyParameters();
           identifyParameters.LayerOption = LayerOption.all;
           identifyParameters.SpatialReference = MyMap.SpatialReference;
          
           // Pass the current Map properties to Identify parameters.
           identifyParameters.MapExtent = MyMap.Extent;
           identifyParameters.Width = (int)MyMap.ActualWidth;
           identifyParameters.Height = (int)MyMap.ActualHeight;
          
           // Identify features at the click point.
           identifyParameters.Geometry = args.MapPoint;

           identifyTask.ExecuteAsync(identifyParameters);

       }

       private List<IdentifyResult> _lastIdentifyResult;
      
       // Populate Querytextbox with the value of FAC_ID when Identify is complete.
       private void IdentifyTask_ExecuteCompleted(object sender, IdentifyEventArgs args)
       {
           QueryTextBox.Text = "";

           if (args.IdentifyResults.Count > 0)
           {
               // Store the list of Identify results.
               _lastIdentifyResult = args.IdentifyResults;
              
              
               foreach (IdentifyResult result in args.IdentifyResults)
               {
                   //This sort of works only instead of returning the primary key, FAC_ID, it returns the SHORT_Name of the facility instead
                   string FACID = string.Format("{0}", _lastIdentifyResult[0].Value);
                   QueryTextBox.Text = FACID;

                   //I have also tried the line below, but get a Error:"An object reference is required for the
                   //non-static field, method, or property 'ESRI.ArcGIS.Client.Tasks.IdentifyResult.Feature.get'"
                   //I pulled this from a project done in VB.net hoping that it might work, but it doesn't in C#
                   //It seems like a statement similar to this is all that I would need though

                   //QueryTextBox.Text = IdentifyResult.Feature.Attributes("FAC_ID");
                  
               }

           }

       }

Thanks in advance for help.  I know there has to be a pretty simple fix to this.
0 Kudos
5 Replies
DaveTimmins
Deactivated User
Hi,

in your last statement //QueryTextBox.Text = IdentifyResult.Feature.Attributes("FAC_ID"); IdentifyResult is null as it is not an assigned variable, looks like you should be using result instead.

QueryTextBox.Text = result.Feature.Attributes("FAC_ID");

Cheers,
0 Kudos
BobNichols
Regular Contributor
Thanks for your reply.  Unfortunately I have tried your suggestion already, but I get an Error: Non-invocable member 'ESRI.ArcGIS.Client.Graphic.Attributes' cannot be used like a method.  Any other suggestions?
0 Kudos
DaveTimmins
Deactivated User
Hi,

it should be square brackets instead of parentheses as it's C#

QueryTextBox.Text = result.Feature.Attributes["FAC_ID"];

Cheers,
0 Kudos
BobNichols
Regular Contributor
I wondered about that, I do however get a different error.  Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?) So hopefully we are headed in the right direction.



EDIT: Before I said that I recieved the same error, I was mistaken and added the new error above.
0 Kudos
BobNichols
Regular Contributor
I think that I have got it all sorted out.  I used your suggestion about the brackets and added a type cast.

string FACID = string.Format("{0}", result.Feature.Attributes["FAC_ID"]);
QueryTextBox.Text = FACID;

I think it should work, I tried testing it but for some reason my map service just went down.  So I need to track that down first.  Thank you for your help.
0 Kudos