This sample demonstrates performing identify operations. To use the sample, simply click anywhere in the United States to identify features. The results will be shown in the drop-down menu and DataGrid in the upper right corner of the application. View the data for different results by selecting them from the drop-down menu.
In the code-behind, an IdentifyTask is to perform the identify operation. The tasks IdentifyParameters specify to query the geometry of the map click andIto query all the layers in the target map service, which enables the returning of results from multiple layers.
<UserControlx:Class="ArcGISWPFSDK.Identify"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"><Gridx:Name="LayoutRoot"Background="White"><Grid.Resources><esri:PictureMarkerSymbolx:Key="DefaultPictureSymbol"OffsetX="35"OffsetY="35"Source="/Assets/Images/i_about.png"/><LinearGradientBrushx:Key="PanelGradient"EndPoint="0.5,1"StartPoint="0.5,0"><LinearGradientBrush.RelativeTransform><TransformGroup><ScaleTransformCenterY="0.5"CenterX="0.5"/><SkewTransformCenterY="0.5"CenterX="0.5"/><RotateTransformAngle="176"CenterY="0.5"CenterX="0.5"/><TranslateTransform/></TransformGroup></LinearGradientBrush.RelativeTransform><GradientStopColor="#FF145787"Offset="0.16"/><GradientStopColor="#FF3D7FAC"Offset="0.502"/><GradientStopColor="#FF88C5EF"Offset="0.984"/></LinearGradientBrush></Grid.Resources><esri:Mapx:Name="MyMap"WrapAround="True"Background="White"Extent="-15000000,2000000,-7000000,8000000"MouseClick="QueryPoint_MouseClick"><esri:ArcGISTiledMapServiceLayerID="StreetMapLayer"Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"/><esri:GraphicsLayerID="MyGraphicsLayer"/></esri:Map><Gridx:Name="IdentifyGrid"HorizontalAlignment="Right"VerticalAlignment="Top"Margin="0,7,7,0"><RectangleFill="{StaticResource PanelGradient}"Stroke="Gray"Margin="-5,1,-2,-6"RadiusX="10"RadiusY="10"><Rectangle.Effect><DropShadowEffect/></Rectangle.Effect></Rectangle><TextBlockx:Name="DataDisplayTitleTop"Text="Click on map to identify a feature"Foreground="White"FontSize="12"Margin="2,8,8,-3"/><StackPanelx:Name="IdentifyResultsPanel"Orientation="Vertical"Margin="15"HorizontalAlignment="Center"VerticalAlignment="Top"Visibility="Collapsed"><ComboBoxx:Name="IdentifyComboBox"MinWidth="150"SelectionChanged="cb_SelectionChanged"Margin="2,10,2,2"></ComboBox><ScrollViewerHorizontalScrollBarVisibility="Hidden"VerticalScrollBarVisibility="Auto"Width="230"MaxHeight="340"><DataGridx:Name="IdentifyDetailsDataGrid"AutoGenerateColumns="False"HeadersVisibility="None"Background="White"><DataGrid.Columns><DataGridTextColumnWidth="95"Binding="{Binding Path=Key}"FontWeight="Bold"IsReadOnly="True"/><DataGridTextColumnWidth="115"Binding="{Binding Path=Value}"IsReadOnly="True"/></DataGrid.Columns></DataGrid></ScrollViewer></StackPanel></Grid></Grid></UserControl>
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Tasks;
namespace ArcGISWPFSDK
{
publicpartialclass Identify : UserControl
{
private List<DataItem> _dataItems = null;
public Identify()
{
InitializeComponent();
}
privatevoid QueryPoint_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs e)
{
ESRI.ArcGIS.Client.Geometry.MapPoint clickPoint = e.MapPoint;
ESRI.ArcGIS.Client.Tasks.IdentifyParameters identifyParams = new IdentifyParameters()
{
Geometry = clickPoint,
MapExtent = MyMap.Extent,
Width = (int)MyMap.ActualWidth,
Height = (int)MyMap.ActualHeight,
LayerOption = LayerOption.visible,
SpatialReference = MyMap.SpatialReference
};
IdentifyTask identifyTask = new IdentifyTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +
"Demographics/ESRI_Census_USA/MapServer");
identifyTask.ExecuteCompleted += IdentifyTask_ExecuteCompleted;
identifyTask.Failed += IdentifyTask_Failed;
identifyTask.ExecuteAsync(identifyParams);
GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
graphicsLayer.ClearGraphics();
ESRI.ArcGIS.Client.Graphic graphic = new ESRI.ArcGIS.Client.Graphic()
{
Geometry = clickPoint,
Symbol = LayoutRoot.Resources["DefaultPictureSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol
};
graphicsLayer.Graphics.Add(graphic);
}
publicvoid ShowFeatures(List<IdentifyResult> results)
{
_dataItems = new List<DataItem>();
if (results != null && results.Count > 0)
{
IdentifyComboBox.Items.Clear();
foreach (IdentifyResult result in results)
{
Graphic feature = result.Feature;
string title = result.Value.ToString() + " (" + result.LayerName + ")";
_dataItems.Add(new DataItem()
{
Title = title,
Data = feature.Attributes
});
IdentifyComboBox.Items.Add(title);
}
// Workaround for bug with ComboBox
IdentifyComboBox.UpdateLayout();
IdentifyComboBox.SelectedIndex = 0;
}
}
void cb_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int index = IdentifyComboBox.SelectedIndex;
if (index > -1)
IdentifyDetailsDataGrid.ItemsSource = _dataItems[index].Data;
}
privatevoid IdentifyTask_ExecuteCompleted(object sender, IdentifyEventArgs args)
{
IdentifyDetailsDataGrid.ItemsSource = null;
if (args.IdentifyResults != null && args.IdentifyResults.Count > 0)
{
IdentifyResultsPanel.Visibility = Visibility.Visible;
ShowFeatures(args.IdentifyResults);
}
else
{
IdentifyComboBox.Items.Clear();
IdentifyComboBox.UpdateLayout();
IdentifyResultsPanel.Visibility = Visibility.Collapsed;
}
}
publicclass DataItem
{
publicstring Title { get; set; }
public IDictionary<string, object> Data { get; set; }
}
void IdentifyTask_Failed(object sender, TaskFailedEventArgs e)
{
MessageBox.Show("Identify failed. Error: " + e.Error);
}
}
}
Imports System.Collections.Generic
Imports System.Windows
Imports System.Windows.Controls
Imports ESRI.ArcGIS.Client
Imports ESRI.ArcGIS.Client.Tasks
Namespace ArcGISWPFSDK
PartialPublicClass Identify
Inherits UserControl
Private _dataItems As List(Of DataItem) = NothingPublicSubNew()
InitializeComponent()
EndSubPrivateSub QueryPoint_MouseClick(sender AsObject, e As ESRI.ArcGIS.Client.Map.MouseEventArgs)
Dim clickPoint As ESRI.ArcGIS.Client.Geometry.MapPoint = e.MapPoint
Dim identifyParams As ESRI.ArcGIS.Client.Tasks.IdentifyParameters = New IdentifyParameters() With { _
.Geometry = clickPoint, _
.MapExtent = MyMap.Extent, _
.Width = CInt(Math.Truncate(MyMap.ActualWidth)), _
.Height = CInt(Math.Truncate(MyMap.ActualHeight)), _
.LayerOption = LayerOption.visible, _
.SpatialReference = MyMap.SpatialReference _
}
Dim identifyTask AsNew IdentifyTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" & "Demographics/ESRI_Census_USA/MapServer")
AddHandler identifyTask.ExecuteCompleted, AddressOf IdentifyTask_ExecuteCompleted
AddHandler identifyTask.Failed, AddressOf IdentifyTask_Failed
identifyTask.ExecuteAsync(identifyParams)
Dim graphicsLayer As GraphicsLayer = TryCast(MyMap.Layers("MyGraphicsLayer"), GraphicsLayer)
graphicsLayer.ClearGraphics()
Dim graphic AsNew ESRI.ArcGIS.Client.Graphic() With { _
.Geometry = clickPoint, _
.Symbol = TryCast(LayoutRoot.Resources("DefaultPictureSymbol"), ESRI.ArcGIS.Client.Symbols.Symbol) _
}
graphicsLayer.Graphics.Add(graphic)
EndSubPublicSub ShowFeatures(results As List(Of IdentifyResult))
_dataItems = New List(Of DataItem)()
If results IsNotNothingAndAlso results.Count > 0 Then
IdentifyComboBox.Items.Clear()
ForEach result As IdentifyResult In results
Dim feature As Graphic = result.Feature
Dim title AsString = result.Value.ToString() & " (" & result.LayerName & ")"
_dataItems.Add(New DataItem() With { _
.Title = title, _
.Data = feature.Attributes _
})
IdentifyComboBox.Items.Add(title)
Next' Workaround for bug with ComboBox
IdentifyComboBox.UpdateLayout()
IdentifyComboBox.SelectedIndex = 0
EndIfEndSubPrivateSub cb_SelectionChanged(sender AsObject, e As SelectionChangedEventArgs)
Dim index AsInteger = IdentifyComboBox.SelectedIndex
If index > -1 Then
IdentifyDetailsDataGrid.ItemsSource = _dataItems(index).Data
EndIfEndSubPrivateSub IdentifyTask_ExecuteCompleted(sender AsObject, args As IdentifyEventArgs)
IdentifyDetailsDataGrid.ItemsSource = NothingIf args.IdentifyResults IsNotNothingAndAlso args.IdentifyResults.Count > 0 Then
IdentifyResultsPanel.Visibility = Visibility.Visible
ShowFeatures(args.IdentifyResults)
Else
IdentifyComboBox.Items.Clear()
IdentifyComboBox.UpdateLayout()
IdentifyResultsPanel.Visibility = Visibility.Collapsed
EndIfEndSubPublicClass DataItem
PublicProperty Title() AsStringGetReturn m_Title
EndGetSet(value AsString)
m_Title = Value
EndSetEndPropertyPrivate m_Title AsStringPublicProperty Data() As IDictionary(Of String, Object)
GetReturn m_Data
EndGetSet(value As IDictionary(Of String, Object))
m_Data = Value
EndSetEndPropertyPrivate m_Data As IDictionary(Of String, Object)
EndClassPrivateSub IdentifyTask_Failed(sender AsObject, e As TaskFailedEventArgs)
MessageBox.Show("Identify failed. Error: " & Convert.ToString(e.[Error]))
EndSubEndClassEndNamespace