
An example of XAML and .NET code (in this case C#) for a simple Silverlight 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 rest of this document will walk you through how the Identify task is defined in the example.
[XAML]
<UserControl x:Class="SilverlightApp.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:esri="clr-namespace:ESRI.ArcGIS.Client;assembly=ESRI.ArcGIS.Client"
xmlns:esriTasks="clr-namespace:ESRI.ArcGIS.Client.Tasks;assembly=ESRI.ArcGIS.Client"
xmlns:esriSymbols="clr-namespace:ESRI.ArcGIS.Client.Symbols;assembly=ESRI.ArcGIS.Client"
xmlns:slData="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" >
<Grid x:Name="LayoutRoot" Background="White" >
<!-- IDENTIFY TASK RESOURCES -->
<Grid.Resources>
<esriSymbols:PictureMarkerSymbol x:Name="IdentifyLocationSymbol" OffsetX="35" OffsetY="35"
Source="/Assets/images/i_about.png" />
<esriSymbols:SimpleFillSymbol x:Name="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,25,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">
<slData:DataGrid x:Name="IdentifyDetailsDataGrid" AutoGenerateColumns="False"
HeadersVisibility="None" >
<slData:DataGrid.Columns>
<slData:DataGridTextColumn Binding="{Binding Path=Key}" FontWeight="Bold"/>
<slData:DataGridTextColumn Binding="{Binding Path=Value}"/>
</slData:DataGrid.Columns>
</slData:DataGrid>
</ScrollViewer>
</StackPanel>
</Grid>
</StackPanel>
</Grid>
</UserControl>
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Collections.Generic;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Tasks;
using ESRI.ArcGIS.Client.Symbols;
namespace SilverlightApp
{
public partial class Page : UserControl
{
private List<IdentifyResults> _lastIdentifyResult;
public Page() { InitializeComponent(); }
// Do identify when 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 = IdentifyLocationSymbol
};
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 searching of all layers.
IdentifyParameters identifyParameters = new IdentifyParameters();
identifyParameters.LayerOption = LayerOption.all;
// Pass 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 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 attribuets 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 user
IdentifyResultsStackPanel.Visibility = Visibility.Collapsed;
MessageBox.Show("No features found");
}
}
// Show geometry and attributes of 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. Needed because SelectionChanged fires
// when ComboBox.Clear is called.
if (IdentifyComboBox.SelectedIndex > -1)
{
// Update DataGrid with selected feature's attributes
Graphic selectedFeature = _lastIdentifyResult[IdentifyComboBox.SelectedIndex].Feature;
IdentifyDetailsDataGrid.ItemsSource = selectedFeature.Attributes;
// Apply symbol and add selected feature to map
selectedFeature.Symbol = SelectedFeatureSymbol;
graphicsLayer.Graphics.Add(selectedFeature);
}
}
// Notify when identify fails
private void IdentifyTask_Failed(object sender, TaskFailedEventArgs args)
{
MessageBox.Show("Identify failed: " + args.Error);
}
}
}The following steps assume you have created a Silverlight application with a map and a base layer as described in Creating a Map. The XAML view of your application's main page (e.g. Page.xaml) should look similar to the following:
<UserControl x:Class="SilverlightApp.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:esri="clr-namespace:ESRI.ArcGIS.Client;assembly=ESRI.ArcGIS.Client">
<Grid x:Name="LayoutRoot" Background="White">
<!-- MAP -->
<esri:Map x:Name="MyMap" Extent="-130,10,-70,60" >
<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>
</UserControl>
The code in the main page's code-behind (e.g. Page.xaml.cs) should be unchanged from when you created your Silverlight application project in Visual Studio.
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:
<StackPanel Margin="10" HorizontalAlignment="Left">
<Grid>
<Rectangle Fill="#CC5C90B2" Stroke="Gray" RadiusX="10" RadiusY="10" />
</Grid>
</StackPanel>
<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>
<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>
<UserControl x:Class="SilverlightApp.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:esri="clr-namespace:ESRI.ArcGIS.Client;assembly=ESRI.ArcGIS.Client"
xmlns:esriTasks="clr-namespace:ESRI.ArcGIS.Client.Tasks;assembly=ESRI.ArcGIS.Client"
xmlns:esriSymbols="clr-namespace:ESRI.ArcGIS.Client.Symbols;assembly=ESRI.ArcGIS.Client" >
<Grid.Resources>
<esriSymbols:PictureMarkerSymbol />
</Grid.Resources>
<Grid.Resources>
<esriSymbols:PictureMarkerSymbol x:Name="IdentifyLocationSymbol" />
</Grid.Resources>
<Grid.Resources>
<esriSymbols:PictureMarkerSymbol x:Name="IdentifyLocationSymbol" OffsetX="35" OffsetY="35"
Source="/Assets/images/i_about.png" />
</Grid.Resources>
<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:Map.Layers>
</esri:Map>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 Silverlight DataGrid to show the selected feature's attributes and a GraphicsLayer to display the selected feature's geometry.
<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>
<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>
<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>
<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>
<UserControl x:Class="SilverlightApp.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:esri="clr-namespace:ESRI.ArcGIS.Client;assembly=ESRI.ArcGIS.Client"
xmlns:esriTasks="clr-namespace:ESRI.ArcGIS.Client.Tasks;assembly=ESRI.ArcGIS.Client"
xmlns:esriSymbols="clr-namespace:ESRI.ArcGIS.Client.Symbols;assembly=ESRI.ArcGIS.Client"
xmlns:slData="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" >
<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" />
<slData:DataGrid x:Name="IdentifyDetailsDataGrid" AutoGenerateColumns="False" >
</slData:DataGrid>
</StackPanel>
</Grid>
</StackPanel>
<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" />
<slData:DataGrid x:Name="IdentifyDetailsDataGrid" AutoGenerateColumns="False"
HeadersVisibility="None" >
</slData:DataGrid>
</StackPanel>
</Grid>
</StackPanel>
<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" />
<slData:DataGrid x:Name="IdentifyDetailsDataGrid" AutoGenerateColumns="False"
HeadersVisibility="None" >
<slData:DataGrid.Columns>
<slData:DataGridTextColumn FontWeight="Bold"/>
<slData:DataGridTextColumn />
</slData:DataGrid.Columns>
</slData:DataGrid>
</StackPanel>
</Grid>
</StackPanel>
<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" />
<slData:DataGrid x:Name="IdentifyDetailsDataGrid" AutoGenerateColumns="False"
HeadersVisibility="None" >
<slData:DataGrid.Columns>
<slData:DataGridTextColumn Binding="{Binding Path=Key}" FontWeight="Bold"/>
<slData:DataGridTextColumn Binding="{Binding Path=Value}"/>
</slData:DataGrid.Columns>
</slData:DataGrid>
</StackPanel>
</Grid>
</StackPanel>
<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">
<slData:DataGrid x:Name="IdentifyDetailsDataGrid" AutoGenerateColumns="False"
HeadersVisibility="None" >
<slData:DataGrid.Columns>
<slData:DataGridTextColumn Binding="{Binding Path=Key}" FontWeight="Bold"/>
<slData:DataGridTextColumn Binding="{Binding Path=Value}"/>
</slData:DataGrid.Columns>
</slData:DataGrid>
</ScrollViewer>
</StackPanel>
</Grid>
</StackPanel>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:
You will implement these components in .NET code contained in the main page'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 Page class in the code-behind file for your Silverlight application's main page (e.g. Page.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 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 Silverlight, XAML is reserved for an application's presentation layer, while the code-behind is where business logic is implemented.
- Declare the MyMap_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.ClearGraphics(); }
- 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 = IdentifyLocationSymbol }; }
- 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 = IdentifyLocationSymbol }; graphicsLayer.Graphics.Add(graphic); }
- Declare and instantiate an Identify task. Set the map service 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 = IdentifyLocationSymbol }; graphicsLayer.Graphics.Add(graphic); IdentifyTask identifyTask = new IdentifyTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" + "Demographics/ESRI_Census_USA/MapServer"); }
- 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.ClearGraphics(); ESRI.ArcGIS.Client.Graphic graphic = new ESRI.ArcGIS.Client.Graphic() { Geometry = args.MapPoint, Symbol = IdentifyLocationSymbol }; graphicsLayer.Graphics.Add(graphic); IdentifyTask identifyTask = new IdentifyTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" + "Demographics/ESRI_Census_USA/MapServer"); 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.ClearGraphics(); ESRI.ArcGIS.Client.Graphic graphic = new ESRI.ArcGIS.Client.Graphic() { Geometry = args.MapPoint, Symbol = IdentifyLocationSymbol }; 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; }
- 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 = IdentifyLocationSymbol }; 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(); }
- 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 = IdentifyLocationSymbol }; 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; }
- 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 = IdentifyLocationSymbol }; 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; }
- 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 = IdentifyLocationSymbol }; 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; }
- 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 = IdentifyLocationSymbol }; 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 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 Page : UserControl { 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 selecton is not valid (e.g. 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.ClearGraphics(); }
- 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) { } }
- 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.ClearGraphics(); 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.ClearGraphics(); 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.ClearGraphics(); if (IdentifyComboBox.SelectedIndex > -1) { Graphic selectedFeature = _lastIdentifyResult[IdentifyComboBox.SelectedIndex].Feature; IdentifyDetailsDataGrid.ItemsSource = selectedFeature.Attributes; selectedFeature.Symbol = SelectedFeatureSymbol; 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); }