Identify task

The Identify task allows you to search the layers in a map for features that intersect an input geometry. Once the matching features are returned, you can use .NET code to display their geometries and attributes in your WPF application. To use an Identify task, you need to include code to define the task's user interface (UI) and specify its execution logic.

An example of XAML and .NET code (in this case C#) for a simple WPF application that includes an Identify task is shown below. This application defines an Identify task that uses the Map control's MouseClick event for specifying the input geometry and executing the task. The list of intersecting features is displayed in a ComboBox. The feature currently selected in the ComboBox has its attributes shown in a DataGrid and its geometry drawn in a GraphicsLayer. The remainder of this topic walks you through how the Identify task is defined in the following example.

NoteNote:

The code sections in this topic require that the project has a reference to the ESRI.ArcGIS.Client assembly. The XAML of the Window (for example, MainWindow.xaml) needs to have the following XML namespace declaration:

xmlns:esri="http://schemas.esri.com/arcgis/client/2009"

The code-behind (for example, MainWindow.xaml.cs) must include the following using statements:

using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Tasks;
using ESRI.ArcGIS.Client.Symbols;

The Grid shown in the following code section replaces the main Grid in the XAML.

<Grid x:Name="LayoutRoot" Background="White" >

  <!-- IDENTIFY TASK RESOURCES -->
  <Grid.Resources>
    <esri:PictureMarkerSymbol x:Key="IdentifyLocationSymbol" OffsetX="35" OffsetY="35"
      Source="/Assets/images/i_about.png" />
    <esri:SimpleFillSymbol x:Key="SelectedFeatureSymbol" Fill="#64FF0000" BorderBrush="Red" 
      BorderThickness="2" />
  </Grid.Resources>

  <!-- MAP -->
  <esri:Map x:Name="MyMap" Extent="-130,10,-70,60" MouseClick="MyMap_MouseClick" >
    <esri:Map.Layers>
      <esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer"
        Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
      <esri:GraphicsLayer ID="ResultsGraphicsLayer" />
      <esri:GraphicsLayer ID="IdentifyIconGraphicsLayer" />
    </esri:Map.Layers>
  </esri:Map>

  <!-- IDENTIFY TASK INTERFACE -->
  <StackPanel Margin="10" HorizontalAlignment="Left">
    <Grid>
      <Rectangle Fill="#CC5C90B2" Stroke="Gray"  RadiusX="10" RadiusY="10" />
      <TextBlock Text="Click the map to identify a feature" Foreground="White" FontSize="10" 
        Margin="10,5,10,5" />
      <StackPanel x:Name="IdentifyResultsStackPanel" Margin="15,30,15,10" Visibility="Collapsed">
        <TextBlock Text="Select a result from the list to display it" Foreground="White" 
          FontSize="10" Margin="0,0,0,5" />
        <ComboBox x:Name="IdentifyComboBox" SelectionChanged="IdentifyComboBox_SelectionChanged" />
        <ScrollViewer MaxHeight="340" Margin="0,10,0,0">
          <DataGrid x:Name="IdentifyDetailsDataGrid" AutoGenerateColumns="False"
            HeadersVisibility="None" >
            <DataGrid.Columns>
              <DataGridTextColumn Binding="{Binding Path=Key}" FontWeight="Bold"/>
              <DataGridTextColumn Binding="{Binding Path=Value}"/>
            </DataGrid.Columns>
          </DataGrid>
        </ScrollViewer>
      </StackPanel>
    </Grid>
  </StackPanel>
</Grid>

The methods shown in the following code section follow the constructor in the code-behind.

private List<IdentifyResult> _lastIdentifyResult;

// Perform Identify when the map is clicked.
private void MyMap_MouseClick(object sender, ESRI.ArcGIS.Client.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("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +
    "Demographics/ESRI_Census_USA/MapServer");
  identifyTask.ExecuteCompleted += IdentifyTask_ExecuteCompleted;
  identifyTask.Failed += IdentifyTask_Failed;

  // Initialize Identify parameters. Specify search of all layers.
  IdentifyParameters identifyParameters = new IdentifyParameters();
  identifyParameters.LayerOption = LayerOption.all;

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

// Populate ComboBox with the results when Identify is complete.
private void IdentifyTask_ExecuteCompleted(object sender, IdentifyEventArgs args)
{
  IdentifyComboBox.Items.Clear();

  // Check for new results.
  if (args.IdentifyResults.Count > 0)
  {
    // Show ComboBox and attributes DataGrid.
    IdentifyResultsStackPanel.Visibility = Visibility.Visible;

    // Add results to ComboBox.
    foreach (IdentifyResult result in args.IdentifyResults)
    {
      string title = string.Format("{0} ({1})", result.Value.ToString(), result.LayerName);
      IdentifyComboBox.Items.Add(title);
    }

    // Workaround for ComboBox bug.
    IdentifyComboBox.UpdateLayout();

    // Store the list of Identify results.
    _lastIdentifyResult = args.IdentifyResults;

    // Initialize ComboBox and fire SelectionChanged.
    IdentifyComboBox.SelectedIndex = 0;
  }
  else
  {
    // Hide ComboBox and attributes DataGrid and notify the user.
    IdentifyResultsStackPanel.Visibility = Visibility.Collapsed;
    MessageBox.Show("No features found");
  }
}

// Show geometry and attributes of the selected feature.
void IdentifyComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
  // Clear previously selected feature from GraphicsLayer.
  GraphicsLayer graphicsLayer = MyMap.Layers["ResultsGraphicsLayer"] as GraphicsLayer;
  graphicsLayer.ClearGraphics();

  // Check that ComboBox has a selected item. This is needed because SelectionChanged fires
  // when ComboBox.Clear is called.
  if (IdentifyComboBox.SelectedIndex > -1)
  {
    // Update DataGrid with the selected feature's attributes.
    Graphic selectedFeature = _lastIdentifyResult[IdentifyComboBox.SelectedIndex].Feature;
    IdentifyDetailsDataGrid.ItemsSource = selectedFeature.Attributes;

    // Apply the symbol and add the selected feature to the map.
    selectedFeature.Symbol = LayoutRoot.Resources["SelectedFeatureSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
    graphicsLayer.Graphics.Add(selectedFeature);
  }
}

// Notify when Identify fails.
private void IdentifyTask_Failed(object sender, TaskFailedEventArgs args)
{
  MessageBox.Show("Identify failed: " + args.Error);
}

NoteNote:

Additional examples of the Identify functionality can be found in the Interactive SDK.

Creating an Identify task

The following steps assume you have created a WPF application with a map and a base layer as described in Creating a map. The XAML view of your application's main window (for example, MainWindow.xaml) should look similar to the following:

<Window x:Class="WPFApp.MainWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:esri="http://schemas.esri.com/arcgis/client/2009"
  Title="MainWindow" Height="350" Width="525">

  <Grid x:Name="LayoutRoot" >

    <!-- MAP -->
    <esri:Map x:Name="MyMap" Extent="-74.18,40.69,-73.89,40.89" >
      <esri:Map.Layers>
        <esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer" 
          Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
      </esri:Map.Layers>
    </esri:Map>
  </Grid>
</Window>

The code in the main window's code-behind (for example, MainWindow.xaml.cs) should be unchanged from when you created your WPF application project in Visual Studio.

Creating an input interface for the Identify task

Since tasks do not define a UI, you must implement an input interface to allow users of your application to perform identify operations. The interface defined by the example can be divided into the following three parts:

  • Information about how to execute the task
  • Specification of input geometry
  • Display of the user-defined input

You'll define each of these in the following steps:

  1. In XAML, specify a Rectangle to use as the background for the task's instructions. Place the Rectangle inside a Grid and the Grid inside a StackPanel. This structure—a Rectangle inside a Grid inside a StackPanel—causes the Rectangle to automatically resize to the visible contents of the Grid. This resizing behavior allows you to also use the Rectangle as the background for the list of results and their attributes.
    <StackPanel Margin="10" HorizontalAlignment="Left">
      <Grid>
        <Rectangle Fill="#CC5C90B2" Stroke="Gray"  RadiusX="10" RadiusY="10" />
      </Grid>
    </StackPanel>
    
  2. Define a TextBox to inform the user about how to execute the Identify task.
    <StackPanel Margin="10" HorizontalAlignment="Left">
      <Grid>
        <Rectangle Fill="#CC5C90B2" Stroke="Gray"  RadiusX="10" RadiusY="10" />
        <TextBlock Text="Click the map to identify a feature" Foreground="White" FontSize="10"
          Margin="10,5,10,5" />
      </Grid>
    </StackPanel>
    
  3. Use the Map's MouseClick event to use a point as the input geometry for the Identify task. This event fires whenever the Map control is clicked. Define the MouseClick attribute within the Map XAML element as shown in the following code:
    <esri:Map x:Name="MyMap" Extent="-130,10,-70,60" MouseClick="MyMap_MouseClick" >
      <esri:Map.Layers>
        <esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer"
          Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
      </esri:Map.Layers>
    </esri:Map>
    
  4. To show the user the location of the identify operation, you'll display an image at the click point. Images can be displayed on a Map control by using a PictureMarkerSymbol, which is included in the ESRI.ArcGIS.Client.Symbols namespace of the ESRI.ArcGIS.Client assembly. Declare a PictureMarkerSymbol as a resource of the root Grid element.
    <Grid.Resources>
      <esri:PictureMarkerSymbol />
    </Grid.Resources>
    
  5. Specify the x:Key attribute for the symbol. This allows you to reference the symbol from the window's code-behind.
    <Grid.Resources>
      <esri:PictureMarkerSymbol x:Key="IdentifyLocationSymbol" />
    </Grid.Resources>
    
  6. Specify the path to the image in the symbol element's Source attribute. You'll also have to add this image to your WPF application project in Visual Studio in the location specified. The OffsetX and OffsetY attributes allow you to specify the location of the image relative to the click point. For the image used in this example, define these as shown:
    <Grid.Resources>
      <esri:PictureMarkerSymbol x:Key="IdentifyLocationSymbol" OffsetX="35" OffsetY="35" 
        Source="/Assets/images/i_about.png" />
    </Grid.Resources>
    
  7. Similarly to the PictureMarkerSymbol, add a SimpleFillSymbol to the Grid Resources.
    <Grid.Resources>
      <esri:PictureMarkerSymbol x:Key="IdentifyLocationSymbol" OffsetX="35" OffsetY="35"
        Source="/Assets/images/i_about.png" />
      <esri:SimpleFillSymbol x:Key="SelectedFeatureSymbol" Fill="#64FF0000" BorderBrush="Red"
        BorderThickness="2" />
    </Grid.Resources>
    
  8. Add two GraphicsLayers to the Map control to use for drawing the PictureMarkerSymbol and SimpleFillSymbol on the map.
    <esri:Map x:Name="MyMap" Extent="-130,10,-70,60" MouseClick="MyMap_MouseClick" >
      <esri:Map.Layers>
        <esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer"
          Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
        <esri:GraphicsLayer ID="IdentifyIconGraphicsLayer" />
        <esri:GraphicsLayer ID="ResultsGraphicsLayer" />
      </esri:Map.Layers>
    </esri:Map>
    

Creating an output interface for the Identify task

To display the results of the Identify task, you need to specify an output interface. Since the result features of an Identify operation will overlap geographically, this example shows how to implement a ComboBox control that allows users to select a single feature to display. You'll define a WPF DataGrid to show the selected feature's attributes and a GraphicsLayer to display the selected feature's geometry.

  1. The Rectangle element you defined earlier as the background for the Identify task's instructions will also be used as the background for the ComboBox and DataGrid. Add a StackPanel to the Rectangle's container element to position the ComboBox and DataGrid and control their visibility.
    <StackPanel Margin="10" HorizontalAlignment="Left">
      <Grid>
        <Rectangle Fill="#CC5C90B2" Stroke="Gray"  RadiusX="10" RadiusY="10" />
        <TextBlock Text="Click the map to identify a feature" Foreground="White" FontSize="10"
          Margin="10,5,10,5" />
        <StackPanel Margin="15,30,15,10">
        </StackPanel>
      </Grid>
    </StackPanel>
    
  2. Within the StackPanel element, specify the x:Name attribute to enable access from the window's code-behind. Define the Visibility attribute as Collapsed so that the uninitialized ComboBox and DataGrid are not visible.
    <StackPanel Margin="10" HorizontalAlignment="Left">
      <Grid>
        <Rectangle Fill="#CC5C90B2" Stroke="Gray"  RadiusX="10" RadiusY="10" />
        <TextBlock Text="Click the map to identify a feature" Foreground="White" FontSize="10"
          Margin="10,5,10,5" />
        <StackPanel x:Name="IdentifyResultsStackPanel" Margin="15,30,15,10" Visibility="Collapsed">
        </StackPanel>
      </Grid>
    </StackPanel>
    
  3. Add a TextBlock to the StackPanel to instruct the user.
    <StackPanel Margin="10" HorizontalAlignment="Left">
      <Grid>
        <Rectangle Fill="#CC5C90B2" Stroke="Gray"  RadiusX="10" RadiusY="10" />
        <TextBlock Text="Click the map to identify a feature" Foreground="White" FontSize="10"
          Margin="10,5,10,5" />
        <StackPanel x:Name="IdentifyResultsStackPanel" Margin="15,30,15,10" Visibility="Collapsed">
          <TextBlock Text="Select a result from the list to display it" Foreground="White" 
            FontSize="10" Margin="0,0,0,5" />
        </StackPanel>
      </Grid>
    </StackPanel>
    
  4. Define the ComboBox for selecting result features, declaring a handler for the ComboBox's SelectionChanged event. The SelectionChanged event fires when the item selected in the ComboBox changes. Later in this topic, you'll implement this handler so that it updates the attributes shown in the DataGrid and the result feature drawn on the map.
    <StackPanel Margin="10" HorizontalAlignment="Left">
      <Grid>
        <Rectangle Fill="#CC5C90B2" Stroke="Gray"  RadiusX="10" RadiusY="10" />
        <TextBlock Text="Click the map to identify a feature" Foreground="White" FontSize="10"
          Margin="10,5,10,5" />
        <StackPanel x:Name="IdentifyResultsStackPanel" Margin="15,30,15,10" Visibility="Collapsed">
          <TextBlock Text="Select a result from the list to display it" Foreground="White" 
            FontSize="10" Margin="0,0,0,5" />
          <ComboBox x:Name="IdentifyComboBox" SelectionChanged="IdentifyComboBox_SelectionChanged" />
        </StackPanel>
      </Grid>
    </StackPanel>
    
  5. In the Identify results StackPanel, declare a DataGrid. You'll declare the grid's column explicitly, so set the AutoGenerateColumns attribute to false.
    <StackPanel Margin="10" HorizontalAlignment="Left">
      <Grid>
        <Rectangle Fill="#CC5C90B2" Stroke="Gray"  RadiusX="10" RadiusY="10" />
        <TextBlock Text="Click the map to identify a feature" Foreground="White" FontSize="10"
          Margin="10,5,10,5" />
        <StackPanel x:Name="IdentifyResultsStackPanel" Margin="15,30,15,10" Visibility="Collapsed">
          <TextBlock Text="Select a result from the list to display it" Foreground="White"
            FontSize="10" Margin="0,0,0,5" />
          <ComboBox x:Name="IdentifyComboBox" SelectionChanged="IdentifyComboBox_SelectionChanged" />
          <DataGrid x:Name="IdentifyDetailsDataGrid" AutoGenerateColumns="False" >
          </DataGrid>
        </StackPanel>
      </Grid>
    </StackPanel>
    
  6. In the DataGrid, the attributes are shown in two columns: one for the attribute field name and the other for the attribute value. Since this format does not use column headers, set the DataGrid's HeaderVisibility to None.
    StackPanel Margin="10" HorizontalAlignment="Left">
      <Grid>
        <Rectangle Fill="#CC5C90B2" Stroke="Gray"  RadiusX="10" RadiusY="10" />
        <TextBlock Text="Click the map to identify a feature" Foreground="White" FontSize="10"
          Margin="10,5,10,5" />
        <StackPanel x:Name="IdentifyResultsStackPanel" Margin="15,30,15,10" Visibility="Collapsed">
          <TextBlock Text="Select a result from the list to display it" Foreground="White"
            FontSize="10" Margin="0,0,0,5" />
          <ComboBox x:Name="IdentifyComboBox" SelectionChanged="IdentifyComboBox_SelectionChanged" />
          <DataGrid x:Name="IdentifyDetailsDataGrid" AutoGenerateColumns="False"
            HeadersVisibility="None" >
          </DataGrid>
        </StackPanel>
      </Grid>
    </StackPanel>
    
  7. Add two columns to the DataGrid: one for the field name and one for the attribute value. Specify a bold font for the field name column.
    <StackPanel Margin="10" HorizontalAlignment="Left">
      <Grid>
        <Rectangle Fill="#CC5C90B2" Stroke="Gray"  RadiusX="10" RadiusY="10" />
        <TextBlock Text="Click the map to identify a feature" Foreground="White" FontSize="10"
          Margin="10,5,10,5" />
        <StackPanel x:Name="IdentifyResultsStackPanel" Margin="15,30,15,10" Visibility="Collapsed">
          <TextBlock Text="Select a result from the list to display it" Foreground="White"
            FontSize="10" Margin="0,0,0,5" />
          <ComboBox x:Name="IdentifyComboBox" SelectionChanged="IdentifyComboBox_SelectionChanged" />
          <DataGrid x:Name="IdentifyDetailsDataGrid" AutoGenerateColumns="False"
            HeadersVisibility="None" >
            <DataGrid.Columns>
              <DataGridTextColumn FontWeight="Bold"/>
              <DataGridTextColumn />
            </DataGrid.Columns>
          </DataGrid>
        </StackPanel>
      </Grid>
    </StackPanel>
    
  8. In the ArcGIS API for WPF, result features are returned as Graphic objects, each of which defines its attributes as a Dictionary. In this attribute dictionary, each item's Key is the attribute name and Value is the attribute value. Since data controls in WPF can be bound to properties of CLR types, you can show the field name in one column and attribute value in the other by explicitly binding to the Key and Value properties as shown in the following code:
    <StackPanel Margin="10" HorizontalAlignment="Left">
      <Grid>
        <Rectangle Fill="#CC5C90B2" Stroke="Gray"  RadiusX="10" RadiusY="10" />
        <TextBlock Text="Click the map to identify a feature" Foreground="White" FontSize="10"
          Margin="10,5,10,5" />
        <StackPanel x:Name="IdentifyResultsStackPanel" Margin="15,30,15,10" Visibility="Collapsed">
          <TextBlock Text="Select a result from the list to display it" Foreground="White"
            FontSize="10" Margin="0,0,0,5" />
          <ComboBox x:Name="IdentifyComboBox" SelectionChanged="IdentifyComboBox_SelectionChanged" />
          <DataGrid x:Name="IdentifyDetailsDataGrid" AutoGenerateColumns="False" 
            HeadersVisibility="None" >
            <DataGrid.Columns>
              <DataGridTextColumn Binding="{Binding Path=Key}" FontWeight="Bold"/>
              <DataGridTextColumn Binding="{Binding Path=Value}"/>
            </DataGrid.Columns>
          </DataGrid>
        </StackPanel>
      </Grid>
    </StackPanel>
    
  9. Embed the DataGrid in a ScrollViewer element. This will limit the height of the DataGrid, which is desirable in this case because the layers in the ESRI_Census_USA map service contain many attributes.
    <StackPanel Margin="10" HorizontalAlignment="Left">
      <Grid>
        <Rectangle Fill="#CC5C90B2" Stroke="Gray"  RadiusX="10" RadiusY="10" />
        <TextBlock Text="Click the map to identify a feature" Foreground="White" FontSize="10"
          Margin="10,5,10,5" />
        <StackPanel x:Name="IdentifyResultsStackPanel" Margin="15,30,15,10" Visibility="Collapsed">
          <TextBlock Text="Select a result from the list to display it" Foreground="White"
            FontSize="10" Margin="0,0,0,5" />
          <ComboBox x:Name="IdentifyComboBox" SelectionChanged="IdentifyComboBox_SelectionChanged" />
          <ScrollViewer MaxHeight="340" Margin="0,10,0,0">
            <DataGrid x:Name="IdentifyDetailsDataGrid" AutoGenerateColumns="False"
              HeadersVisibility="None" >
              <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Path=Key}" FontWeight="Bold"/>
                <DataGridTextColumn Binding="{Binding Path=Value}"/>
              </DataGrid.Columns>
            </DataGrid>
          </ScrollViewer>
        </StackPanel>
      </Grid>
    </StackPanel>
    

Implementing the Identify task's execution logic

Now that you've specified the Identify task's UI, you need to define its execution logic. The execution logic can be divided into the following three parts:

  • Task execution
  • Task results display
  • Execution error handling

You'll implement these components in .NET code contained in the main window's code-behind. This code is linked to the XAML presentation layer by manipulating elements that you declared in XAML with x:Name or ID attributes and implementing methods that you declared in XAML as event handlers.

The following steps assume that you are adding code to the Window class in the code-behind file for your WPF application's main window (for example, MainWindow.xaml.cs). In this example, C# is used.

Executing the task

In the application's XAML, you declared the MyMap_MouseClick method as a handler for the Map's MouseClick event. Now you'll implement this handler in the window's code-behind. When you're done, the handler will display an icon at the clicked location, instantiate the task and configure its input parameters, and execute the task.

The task is declared and initialized in the code-behind because tasks alone do not define any UI, but rather encapsulate pieces of execution logic. In WPF, XAML is reserved for an application's presentation layer, while the code-behind is where business logic is implemented.

  1. Declare the MyMap_MouseClick method.
    private void MyMap_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs args)
    {            
    }
    
  2. Retrieve the GraphicsLayer for the Identify icon and clear it of any previously drawn symbols.
    private void MyMap_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs args)
    {
      GraphicsLayer graphicsLayer = MyMap.Layers["IdentifyIconGraphicsLayer"] as GraphicsLayer;
      graphicsLayer.ClearGraphics();
    }
    
  3. Instantiate a new Graphic. Set its geometry to be the point clicked on the map and its symbol to be the PictureMarkerSymbol resource that references the Identify icon.
    private void MyMap_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs args)
    {
      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
      };
    }
    
  4. Add the Identify graphic to the GraphicsLayer.
    private void MyMap_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs args)
    {
      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);
    }
    
  5. Declare and instantiate an Identify task. Set the map service so that the task will search by passing the service's URL to the Identify task's constructor. To find the URL, you can use the ArcGIS Services Directory. See the Discovering Services topic for more information. This example uses the states layer of the ESRI_Census_USA service.
    private void MyMap_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs args)
    {
      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);
    
      IdentifyTask identifyTask = new IdentifyTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +
        "Demographics/ESRI_Census_USA/MapServer");
    }
    
  6. Specify a handler for the task's ExecuteCompleted event. The method specified will be called when the Identify task is done executing. You'll implement this handler in the Displaying results section.
    private void MyMap_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs args)
    {
      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);
    
      IdentifyTask identifyTask = new IdentifyTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +
        "Demographics/ESRI_Census_USA/MapServer");
      identifyTask.ExecuteCompleted += IdentifyTask_ExecuteCompleted;
    }
    
  7. Specify a handler for the task's Failed event, which fires when there is a problem executing the task. You'll define this handler in the Handling execution errors section.
    private void MyMap_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs args)
    {
      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);
    
      IdentifyTask identifyTask = new IdentifyTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +
        "Demographics/ESRI_Census_USA/MapServer");
      identifyTask.ExecuteCompleted += IdentifyTask_ExecuteCompleted;
      identifyTask.Failed += IdentifyTask_Failed;
    }
    
  8. Instantiate a new IdentifyParameters object. The IdentifyParameters object is used to specify the input for Identify tasks.
    private void MyMap_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs args)
    {
      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);
    
      IdentifyTask identifyTask = new IdentifyTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +
        "Demographics/ESRI_Census_USA/MapServer");
      identifyTask.ExecuteCompleted += IdentifyTask_ExecuteCompleted;
      identifyTask.Failed += IdentifyTask_Failed;
    
      IdentifyParameters identifyParameters = new IdentifyParameters();
    }
    
  9. Specify that all the map service's layers be searched. The LayerOption parameter can also be set to search only the top-most or visible layers.
    private void MyMap_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs args)
    {
      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);
    
      IdentifyTask identifyTask = new IdentifyTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +
        "Demographics/ESRI_Census_USA/MapServer");
      identifyTask.ExecuteCompleted += IdentifyTask_ExecuteCompleted;
      identifyTask.Failed += IdentifyTask_Failed;
    
      IdentifyParameters identifyParameters = new IdentifyParameters();
      identifyParameters.LayerOption = LayerOption.all;
    }
    
  10. Use the Map control's properties to initialize the map extent, width, and height of the Identify parameters.
    private void MyMap_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs args)
    {
      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);
    
      IdentifyTask identifyTask = new IdentifyTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +
        "Demographics/ESRI_Census_USA/MapServer");
      identifyTask.ExecuteCompleted += IdentifyTask_ExecuteCompleted;
      identifyTask.Failed += IdentifyTask_Failed;
    
      IdentifyParameters identifyParameters = new IdentifyParameters();
      identifyParameters.LayerOption = LayerOption.all;
    
      identifyParameters.MapExtent = MyMap.Extent;
      identifyParameters.Width = (int)MyMap.ActualWidth;
      identifyParameters.Height = (int)MyMap.ActualHeight;
    }
    
  11. Set the search geometry for the Identify task to be the point clicked on the map.
    private void MyMap_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs args)
    {
      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);
    
      IdentifyTask identifyTask = new IdentifyTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +
        "Demographics/ESRI_Census_USA/MapServer");
      identifyTask.ExecuteCompleted += IdentifyTask_ExecuteCompleted;
      identifyTask.Failed += IdentifyTask_Failed;
    
      IdentifyParameters identifyParameters = new IdentifyParameters();
      identifyParameters.LayerOption = LayerOption.all;
    
      identifyParameters.MapExtent = MyMap.Extent;
      identifyParameters.Width = (int)MyMap.ActualWidth;
      identifyParameters.Height = (int)MyMap.ActualHeight;
    
      identifyParameters.Geometry = args.MapPoint;
    }
    
  12. Execute the Identify task.
    private void MyMap_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs args)
    {
      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);
    
      IdentifyTask identifyTask = new IdentifyTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +
        "Demographics/ESRI_Census_USA/MapServer");
      identifyTask.ExecuteCompleted += IdentifyTask_ExecuteCompleted;
      identifyTask.Failed += IdentifyTask_Failed;
    
      IdentifyParameters identifyParameters = new IdentifyParameters();
      identifyParameters.LayerOption = LayerOption.all;
    
      identifyParameters.MapExtent = MyMap.Extent;
      identifyParameters.Width = (int)MyMap.ActualWidth;
      identifyParameters.Height = (int)MyMap.ActualHeight;
    
      identifyParameters.Geometry = args.MapPoint;
    
      identifyTask.ExecuteAsync(identifyParameters);
    }
    

Displaying results

In the handler for the Map's MouseClick event, you specified IdentifyTask_ExecuteCompleted as the handler for the task's ExecuteCompleted event. This event receives the Identify task's results, which consist of information about all the features in the specified search layers (all, visible, or top-most) that intersect the search geometry. In the main window's XAML, you also declared a ComboBox to hold the results features, a DataGrid to display the selected feature's attributes, and a GraphicsLayer to show the selected feature's geometry. On the ComboBox, you specified the IdentifyComboBox_SelectionChanged method as the handler for the ComboBox's SelectionChanged event.

In this section, you'll implement the ExecuteCompleted handler to populate the Identify ComboBox with a list of the features' display values. Then, you'll implement the SelectionChanged handler to display the attributes of the ComboBox's selected feature in the DataGrid and the geometry of that feature on the GraphicsLayer.

  1. Declare a handler for the Identify task's ExecuteCompleted event. This handler will be invoked when an identify operation is complete. A list of IdentifyResults containing information about the features with geometries intersecting the search geometry is passed to the handler's args parameter. Each IdentifyResult contains the feature found, the name and ID of the layer containing the feature, the value of the feature's display field, and other information.
    private void IdentifyTask_ExecuteCompleted(object sender, IdentifyEventArgs args)
    {
    }
    
  2. Remove previous results from the Identify ComboBox and check whether any results were found for the current operation.
    private void IdentifyTask_ExecuteCompleted(object sender, IdentifyEventArgs args)
    {
      IdentifyComboBox.Items.Clear();
    
      if (args.IdentifyResults.Count > 0)
      {
      }
      else
      {
      }
    }
    
  3. If results were found, make the StackPanel containing the Identify ComboBox and results DataGrid visible.
    private void IdentifyTask_ExecuteCompleted(object sender, IdentifyEventArgs args)
    {
      IdentifyComboBox.Items.Clear();
    
      if (args.IdentifyResults.Count > 0)
      {
        IdentifyResultsStackPanel.Visibility = Visibility.Visible;
      }
      else
      {
      }
    }
    
  4. Loop through the result features. For each one, add its display value and layer to the Identify ComboBox. Then call the ComboBox's UpdateLayout method to apply the updates.
    private void IdentifyTask_ExecuteCompleted(object sender, IdentifyEventArgs args)
    {
      IdentifyComboBox.Items.Clear();
    
      if (args.IdentifyResults.Count > 0)
      {
        IdentifyResultsStackPanel.Visibility = Visibility.Visible;
    
        foreach (IdentifyResult result in args.IdentifyResults)
        {
          string title = string.Format("{0} ({1})", result.Value.ToString(), result.LayerName);
          IdentifyComboBox.Items.Add(title);
        }
    
        IdentifyComboBox.UpdateLayout();
      }
      else
      {
      }
    }
    
  5. At the top of the main window's class, declare an IdentifyResults member variable. This will be used to store the most recently returned set of task results for use when a new result is selected from the ComboBox.
    public partial class MainWindow : Window
    {
      private List<IdentifyResult> _lastIdentifyResult;
    
  6. Store the Identify task's results in the member variable.
    private void IdentifyTask_ExecuteCompleted(object sender, IdentifyEventArgs args)
    {
      IdentifyComboBox.Items.Clear();
    
      if (args.IdentifyResults.Count > 0)
      {
        IdentifyResultsStackPanel.Visibility = Visibility.Visible;
    
        foreach (IdentifyResult result in args.IdentifyResults)
        {
          string title = string.Format("{0} ({1})", result.Value.ToString(), result.LayerName);
          IdentifyComboBox.Items.Add(title);
        }
    
        IdentifyComboBox.UpdateLayout();
    
        _lastIdentifyResult = args.IdentifyResults;
      }
      else
      {
      }
    }
    
  7. Initialize the SelectedIndex of the Identify ComboBox so that the first item in the list is displayed. This will also fire the ComboBox's SelectionChanged event, which you'll implement to update the Identify results DataGrid and draw the selected feature on the map.
    private void IdentifyTask_ExecuteCompleted(object sender, IdentifyEventArgs args)
    {
      IdentifyComboBox.Items.Clear();
    
      if (args.IdentifyResults.Count > 0)
      {
        IdentifyResultsStackPanel.Visibility = Visibility.Visible;
    
        foreach (IdentifyResult result in args.IdentifyResults)
        {
          string title = string.Format("{0} ({1})", result.Value.ToString(), result.LayerName);
          IdentifyComboBox.Items.Add(title);
        }
    
        IdentifyComboBox.UpdateLayout();
    
        _lastIdentifyResult = args.IdentifyResults;
    
        IdentifyComboBox.SelectedIndex = 0;
      }
      else
      {
      }
    }
    
  8. If no features were found, hide the StackPanel containing the Identify ComboBox and DataGrid. Notify the user with a MessageBox.
    private void IdentifyTask_ExecuteCompleted(object sender, IdentifyEventArgs args)
    {
      IdentifyComboBox.Items.Clear();
    
      if (args.IdentifyResults.Count > 0)
      {
        IdentifyResultsStackPanel.Visibility = Visibility.Visible;
    
        foreach (IdentifyResult result in args.IdentifyResults)
        {
          string title = string.Format("{0} ({1})", result.Value.ToString(), result.LayerName);
          IdentifyComboBox.Items.Add(title);
        }
    
        IdentifyComboBox.UpdateLayout();
    
        _lastIdentifyResult = args.IdentifyResults;
    
        IdentifyComboBox.SelectedIndex = 0;
      }
      else
      {
        IdentifyResultsStackPanel.Visibility = Visibility.Collapsed;
        MessageBox.Show("No features found");
      }
    }
    
  9. Declare the IdentifyComboBox_SelectionChanged method. In the window's XAML, you specified this method as the handler for the IdentifyComboBox's SelectionChanged event. The SelectionChanged event fires whenever the selected item in the ComboBox is changed. This includes both interactive and programmatic changes to the selected item, even when the selection is not valid (for example, the ComboBox is cleared).
    void IdentifyComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
    }
    
  10. Retrieve the GraphicsLayer for displaying the currently selected feature and clear it of any previously displayed results.
    void IdentifyComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
      GraphicsLayer graphicsLayer = MyMap.Layers["ResultsGraphicsLayer"] as GraphicsLayer;
      graphicsLayer.ClearGraphics();
    }
    
  11. Check whether an item is currently selected. If no item is selected, the ComboBox's SelectedIndex will be -1.
    void IdentifyComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
      GraphicsLayer graphicsLayer = MyMap.Layers["ResultsGraphicsLayer"] as GraphicsLayer;
      graphicsLayer.ClearGraphics();
    
      if (IdentifyComboBox.SelectedIndex > -1)
      {
      }
    }
    
  12. If an item is selected, get the Graphic (that is, feature) corresponding to that item. For this, the LastResult property on the Identify task is useful. This property holds the set of results returned by the most recently executed identify operation.
    void IdentifyComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
      GraphicsLayer graphicsLayer = MyMap.Layers["ResultsGraphicsLayer"] as GraphicsLayer;
      graphicsLayer.ClearGraphics();
    
      if (IdentifyComboBox.SelectedIndex > -1)
      {
        Graphic selectedFeature = _lastIdentifyResult[IdentifyComboBox.SelectedIndex].Feature;
      }
    }
    
  13. Update the Identify DataGrid to show the attributes of the selected feature. In the window's XAML, you specified two columns in the DataGrid and that they be bound to properties called Key and Value. A Graphic object keeps its attribute in a Dictionary, which is simply a list of key/value pairs—each item defines Key and Value properties. You can bind the DataGrid to the selected Graphic (feature) by passing the Attributes Dictionary to the DataGrid's ItemsSource property.
    void IdentifyComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
      GraphicsLayer graphicsLayer = MyMap.Layers["ResultsGraphicsLayer"] as GraphicsLayer;
      graphicsLayer.ClearGraphics();
    
      if (IdentifyComboBox.SelectedIndex > -1)
      {
        Graphic selectedFeature = _lastIdentifyResult[IdentifyComboBox.SelectedIndex].Feature;
        IdentifyDetailsDataGrid.ItemsSource = selectedFeature.Attributes;
      }
    }
    
  14. Apply the fill symbol you defined in the window's XAML to the selected feature and add the feature to the ResultsGraphicsLayer.
    void IdentifyComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
      GraphicsLayer graphicsLayer = MyMap.Layers["ResultsGraphicsLayer"] as GraphicsLayer;
      graphicsLayer.ClearGraphics();
    
      if (IdentifyComboBox.SelectedIndex > -1)
      {
        Graphic selectedFeature = _lastIdentifyResult[IdentifyComboBox.SelectedIndex].Feature;
        IdentifyDetailsDataGrid.ItemsSource = selectedFeature.Attributes;
    
        selectedFeature.Symbol = LayoutRoot.Resources["SelectedFeatureSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
        graphicsLayer.Graphics.Add(selectedFeature);
      }
    }
    

Handling execution errors

  1. Declare a handler for the Identify task's Failed event. This handler will be invoked if there is a problem with executing an identify operation.
    private void IdentifyTask_Failed(object sender, TaskFailedEventArgs args)
    {
    }
    
  2. Notify the user of the problem with a MessageBox.
    private void IdentifyTask_Failed(object sender, TaskFailedEventArgs args)
    {
      MessageBox.Show("Identify failed: " + args.Error);
    }
    
1/23/2012