How to create an Identify task
The Identify task with online data
The following steps assume you have created a WPF application with a local map. The XAML view of your application's main page (e.g. MainWindow.xaml) should look similar to the following where the xaml is commented out depending if you are using local or online data. Online data is commented out in these examples as local data is demonstrated :
<Window x:Class="ArcGISWpfApplication1.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>
<esri:Map x:Name="MyMap" UseAcceleratedDisplay="True">
<!-- ArcGIS Online Tiled Basemap Layer -->
<!--<esri:ArcGISTiledMapServiceLayer ID="World Topo Map"
Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer"/>-->
<!--Local Dynamic Layer-->
<esri:ArcGISLocalDynamicMapServiceLayer ID="USA"
Path="C:\Program Files (x86)\ArcGIS SDKs\WPF10.2.5\sdk\samples\data\mpks\USCitiesStates.mpk"/>
</esri:Map>
</Grid>
</Window>
The code in the main page's code-behind (e.g. 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 user interface, you must implement an input interface to allow users of your application to perform identify operations. The interface defined by the example can be thought of as three parts:
- Information about how to execute the task
- Specification of input geometry
- Display of the user-defined input
You will define each of these in the steps below.
- 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 will allow 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>
- 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>
- 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's XAML element as shown below.
<esri:Map x:Name="MyMap" UseAcceleratedDisplay="True" MouseClick="MyMap_MouseClick"> <!-- ArcGIS Online Tiled Basemap Layer --> <!--<esri:ArcGISTiledMapServiceLayer ID="World Topo Map" Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer"/>--> <!--Local Dynamic Layer--> <esri:ArcGISLocalDynamicMapServiceLayer ID="USA" Path="C:\Program Files (x86)\ArcGIS SDKs\WPF10.2.5\sdk\samples\data\mpks\USCitiesStates.mpk"/> </esri:Map>
- To show the user the location of the identify operation, you will 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. To use a PictureMarkerSymbol in XAML, you must first add an XML namespace reference to the ESRI.ArcGIS.Client.Symbols namespace.
<Window x:Class="ArcGISWpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:esriTasks="clr-namespace:ESRI.ArcGIS.Client.Tasks;assembly=ESRI.ArcGIS.Client" Title="MainWindow" Height="350" Width="525">
- Now declare a PictureMarkerSymbol as a resource of the root Grid element
<Grid.Resources> <esri:PictureMarkerSymbol /> </Grid.Resources>
- Specify the "x:Name" attribute for the symbol. This allows you to reference the symbol from the page's code-behind.
<Grid.Resources> <esri:PictureMarkerSymbol x:Name="IdentifyLocationSymbol" /> </Grid.Resources>
- Specify the path to the image in the symbol element's Source attribute. Note that you will 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:Name="IdentifyLocationSymbol" x:Key="IdentifylocationSymbol" OffsetX="35" OffsetY="35" Source="http://static.arcgis.com/images/Symbols/Basic/BlueShinyPin.png" /> </Grid.Resources>
- Similarly to the PictureMarkerSymbol, add a SimpleFillSymbol to the Grid Resources.
<Grid.Resources> <esri:PictureMarkerSymbol x:Name="IdentifyLocationSymbol" x:Key="SelectedFeatureSymbol" OffsetX="35" OffsetY="35" Source="http://static.arcgis.com/images/Symbols/Basic/BlueShinyPin.png" /> <esri:SimpleFillSymbol x:Name="SelectedFeatureSymbol" x:Key="SelectedFeatureSymbol" Fill="#64FF0000" BorderBrush="Red" BorderThickness="2" /> </Grid.Resources>
- Add two GraphicsLayers to the Map control to use for drawing the PictureMarkerSymbol and SimpleFillSymbol on the map.
<esri:Map x:Name="MyMap" UseAcceleratedDisplay="True" MouseClick="MyMap_MouseClick"> <!-- ArcGIS Online Tiled Basemap Layer --> <!--<esri:ArcGISTiledMapServiceLayer ID="World Topo Map" Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer"/>--> <!--Local Dynamic Layer--> <esri:ArcGISLocalDynamicMapServiceLayer ID="USA" Path="C:\Program Files (x86)\ArcGIS SDKs\10.2.5\sdk\samples\data\mpks\USCitiesStates.mpk"/>--> <esri:GraphicsLayer ID="IdentifyIconGraphicsLayer" /> <esri:GraphicsLayer ID="ResultsGraphicsLayer" /> </esri:Map>
Creating an output interface for the Identify task
To display the Identify task's results, you need to specify an output interface. Since the result features of an Identify operation will overlap geographically, this example shows you how to implement a ComboBox control that allows users to select a single feature to display. You will define a WPF DataGrid to show the selected feature's attributes and a GraphicsLayer to display the selected feature's geometry
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>
Within the StackPanel element, specify the "x:Name" attribute to enable access from the page'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>
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>
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, you will 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>
Inside the Identify results StackPanel, declare a DataGrid. You will 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>
In the DataGrid, the attributes will be 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>
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>
In the ArcGIS Runtime SDK 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 below.
<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>
Embed the DataGrid in a ScrollViewer element. This will limit the height of the DataGrid.
<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 user interface, you need to define its execution logic. This execution logic can be divided into three parts:
- Task execution
- Task results display
- Execution error handling
You will 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 steps below assume that you are adding code to the Window class in the code-behind file for your WPF application's main window (e.g. MainWindow.xaml.cs). In this example, C# is used.
Executing the task
In the application's XAML, you declared the _map_MouseClick method as a handler for the Map's MouseClick event. Now you will implement this handler in the page's code-behind. When you are 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 user interface, 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.
- Declare the _map_MouseClick method.
private void MyMap_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs args) { }
- 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.Graphics.Clear(); }
- 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.Graphics.Clear(); ESRI.ArcGIS.Client.Graphic graphic = new ESRI.ArcGIS.Client.Graphic() { Geometry = args.MapPoint, Symbol = Resources["IdentifyLocationSymbol"] as ESRI.ArcGIS.Client.Symbols.PictureMarkerSymbol }; }
- 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.Graphics.Clear(); ESRI.ArcGIS.Client.Graphic graphic = new ESRI.ArcGIS.Client.Graphic() { Geometry = args.MapPoint, Symbol = Resources["IdentifyLocationSymbol"] as ESRI.ArcGIS.Client.Symbols.PictureMarkerSymbol }; graphicsLayer.Graphics.Add(graphic); }
- 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 for Online data, you can use the ArcGIS Services Directory. This example uses the states layer of the ESRI_Census_USA service for online data. For local data you will need to declare and instantiate a ArcGISLocalDynamicMapServiceLayer and Initialise this layer. You will then need to set the ArcGISLocalDynamicMapServiceLayer Url as the IdentifyTask Url.
private void MyMap_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs args) { GraphicsLayer graphicsLayer = MyMap.Layers["IdentifyIconGraphicsLayer"] as GraphicsLayer; graphicsLayer.Graphics.Clear(); ESRI.ArcGIS.Client.Graphic graphic = new ESRI.ArcGIS.Client.Graphic() { Geometry = args.MapPoint, Symbol = Resources["IdentifyLocationSymbol"] as ESRI.ArcGIS.Client.Symbols.PictureMarkerSymbol }; graphicsLayer.Graphics.Add(graphic); <!--Online data--> IdentifyTask identifyTask = new IdentifyTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" + "Demographics/ESRI_Census_USA/MapServer"); <!--Local data--> LocalMapService localMapService = new LocalMapService(@"Path to ArcGIS map package"); IdentifyTask identifyTask = new IdentifyTask(); localMapService.StartAsync(delegateService => { identifyTask.Url = localMapService.UrlMapService; }); }
- Specify a handler for the task's ExecuteCompleted event. The method specified will be called when the Identify task is done executing. You will 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.Graphics.Clear(); ESRI.ArcGIS.Client.Graphic graphic = new ESRI.ArcGIS.Client.Graphic() { Geometry = args.MapPoint, Symbol = Resources["IdentifyLocationSymbol"] as ESRI.ArcGIS.Client.Symbols.PictureMarkerSymbol }; graphicsLayer.Graphics.Add(graphic); <!--Online data--> IdentifyTask identifyTask = new IdentifyTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" + "Demographics/ESRI_Census_USA/MapServer"); identifyTask.ExecuteCompleted += IdentifyTask_ExecuteCompleted; <!--Local data--> LocalMapService localMapService = new LocalMapService(@"Path to ArcGIS map package"); IdentifyTask identifyTask = new IdentifyTask(); localMapService.StartAsync(delegateService => { identifyTask.Url = localMapService.UrlMapService; identifyTask.ExecuteCompleted += IdentifyTask_ExecuteCompleted; }); }
- Specify a handler for the task's Failed event, which fires when there is a problem executing the task. You will 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.Graphics.Clear(); ESRI.ArcGIS.Client.Graphic graphic = new ESRI.ArcGIS.Client.Graphic() { Geometry = args.MapPoint, Symbol = Resources["IdentifyLocationSymbol"] as ESRI.ArcGIS.Client.Symbols.PictureMarkerSymbol }; graphicsLayer.Graphics.Add(graphic); <!--Online data--> IdentifyTask identifyTask = new IdentifyTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" + "Demographics/ESRI_Census_USA/MapServer"); identifyTask.ExecuteCompleted += IdentifyTask_ExecuteCompleted; identifyTask.Failed += IdentifyTask_Failed; <!--Local data--> LocalMapService localMapService = new LocalMapService(@"Path to ArcGIS map package"); IdentifyTask identifyTask = new IdentifyTask(); localMapService.StartAsync(delegateService => { identifyTask.Url = localMapService.UrlMapService; identifyTask.ExecuteCompleted += IdentifyTask_ExecuteCompleted; identifyTask.Failed += IdentifyTask_Failed; }); }
- 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.Graphics.Clear(); ESRI.ArcGIS.Client.Graphic graphic = new ESRI.ArcGIS.Client.Graphic() { Geometry = args.MapPoint, Symbol = Resources["IdentifyLocationSymbol"] as ESRI.ArcGIS.Client.Symbols.PictureMarkerSymbol }; graphicsLayer.Graphics.Add(graphic); <!--Online data--> 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(); <!--Local data--> LocalMapService localMapService = new LocalMapService(@"Path to ArcGIS map package"); IdentifyTask identifyTask = new IdentifyTask(); localMapService.StartAsync(delegateService => { identifyTask.Url = localMapService.UrlMapService; identifyTask.ExecuteCompleted += IdentifyTask_ExecuteCompleted; identifyTask.Failed += IdentifyTask_Failed; }); IdentifyParameters identifyParameters = new IdentifyParameters(); }
- 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.Graphics.Clear(); ESRI.ArcGIS.Client.Graphic graphic = new ESRI.ArcGIS.Client.Graphic() { Geometry = args.MapPoint, Symbol = Resources["IdentifyLocationSymbol"] as ESRI.ArcGIS.Client.Symbols.PictureMarkerSymbol }; graphicsLayer.Graphics.Add(graphic); <!--Online data--> 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; <!--Local data--> LocalMapService localMapService = new LocalMapService(@"Path to ArcGIS map package"); IdentifyTask identifyTask = new IdentifyTask(); localMapService.StartAsync(delegateService => { identifyTask.Url = localMapService.UrlMapService; identifyTask.ExecuteCompleted += IdentifyTask_ExecuteCompleted; identifyTask.Failed += IdentifyTask_Failed; }); IdentifyParameters identifyParameters = new IdentifyParameters(); identifyParameters.LayerOption = LayerOption.all; }
- 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.Graphics.Clear(); ESRI.ArcGIS.Client.Graphic graphic = new ESRI.ArcGIS.Client.Graphic() { Geometry = args.MapPoint, Symbol = Resources["IdentifyLocationSymbol"] as ESRI.ArcGIS.Client.Symbols.PictureMarkerSymbol }; graphicsLayer.Graphics.Add(graphic); <!--Online data--> 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; <!--Local data--> LocalMapService localMapService = new LocalMapService(@"Path to ArcGIS map package"); IdentifyTask identifyTask = new IdentifyTask(); localMapService.StartAsync(delegateService => { identifyTask.Url = localMapService.UrlMapService; 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; }
- 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.Graphics.Clear(); ESRI.ArcGIS.Client.Graphic graphic = new ESRI.ArcGIS.Client.Graphic() { Geometry = args.MapPoint, Symbol = Resources["IdentifyLocationSymbol"] as ESRI.ArcGIS.Client.Symbols.PictureMarkerSymbol }; graphicsLayer.Graphics.Add(graphic); <!--Online data--> 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; <!--Local data--> LocalMapService localMapService = new LocalMapService(@"Path to ArcGIS map package"); IdentifyTask identifyTask = new IdentifyTask(); localMapService.StartAsync(delegateService => { identifyTask.Url = localMapService.UrlMapService; 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; }
- Execute the Identify task.
private void MyMap_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs args) { GraphicsLayer graphicsLayer = MyMap.Layers["IdentifyIconGraphicsLayer"] as GraphicsLayer; graphicsLayer.Graphics.Clear(); ESRI.ArcGIS.Client.Graphic graphic = new ESRI.ArcGIS.Client.Graphic() { Geometry = args.MapPoint, Symbol = Resources["IdentifyLocationSymbol"] as ESRI.ArcGIS.Client.Symbols.PictureMarkerSymbol }; graphicsLayer.Graphics.Add(graphic); <!--Online data--> 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); <!--Local data--> LocalMapService localMapService = new LocalMapService(@"Path to ArcGIS map package"); IdentifyTask identifyTask = new IdentifyTask(); localMapService.StartAsync(delegateService => { identifyTask.Url = localMapService.UrlMapService; 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 page's XAML, recall that 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 will implement the ExecuteCompleted handler to populate the Identify ComboBox with a list of the features' display values. Then you will 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.
- 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) { }
- 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 { } }
- 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 { } }
- 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 { } }
- At the top of the main page'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; . . . }
- 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 { } }
- 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 will 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 { } }
- 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"); } }
- Declare the IdentifyComboBox_SelectionChanged method. In the page'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. Note 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) { }
- 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.Graphics.Clear(); }
- 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.Graphics.Clear(); if (IdentifyComboBox.SelectedIndex > -1) { } }
- If an item is selected, get the Graphic (i.e. 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.Graphics.Clear(); if (IdentifyComboBox.SelectedIndex > -1) { Graphic selectedFeature = _lastIdentifyResult[IdentifyComboBox.SelectedIndex].Feature; } }
- Update the Identify DataGrid to show the attributes of the selected feature. In the page's XAML, recall that 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 thus bind the DataGrid to the selected Graphic (i.e. feature) by passing this attributes Dictionary to the DataGrid's ItemsSource property.
void IdentifyComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { GraphicsLayer graphicsLayer = MyMap.Layers["ResultsGraphicsLayer"] as GraphicsLayer; graphicsLayer.Graphics.Clear(); if (IdentifyComboBox.SelectedIndex > -1) { Graphic selectedFeature = _lastIdentifyResult[IdentifyComboBox.SelectedIndex].Feature; IdentifyDetailsDataGrid.ItemsSource = selectedFeature.Attributes; } }
- Apply the fill symbol you defined in the page's XAML to the selected feature and add the feature to the results GraphicsLayer.
void IdentifyComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { GraphicsLayer graphicsLayer = MyMap.Layers["ResultsGraphicsLayer"] as GraphicsLayer; graphicsLayer.Graphics.Clear(); if (IdentifyComboBox.SelectedIndex > -1) { Graphic selectedFeature = _lastIdentifyResult[IdentifyComboBox.SelectedIndex].Feature; IdentifyDetailsDataGrid.ItemsSource = selectedFeature.Attributes; selectedFeature.Symbol = Resources["SelectedFeatureSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol; graphicsLayer.Graphics.Add(selectedFeature); } }
Handling execution errors
- 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) { }
- Notify the user of the problem with a MessageBox
private void IdentifyTask_Failed(object sender, TaskFailedEventArgs args) { MessageBox.Show("Identify failed: " + args.Error); }