This sample demonstrates using the ArcGIS Runtime SDK for WPF to perform identify operations on local data. 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, a LocalMapService is started and the Url property is used to create an IdentifyTask to perform the identify operation. The tasks IdentifyParameters are specified to query the geometry of the map click and to query all the layers in the local map service, which enables the returning of results from multiple layers.
<UserControlx:Class="ArcGISWPFSDK.LocalIdentify"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><BooleanToVisibilityConverterx:Key="BooleanToVisibilityConverter"/><esri:SimpleRendererx:Key="MySimplePolygonRenderer"><esri:SimpleRenderer.Symbol><esri:SimpleFillSymbolFill="#440000FF"/></esri:SimpleRenderer.Symbol></esri:SimpleRenderer><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"MinimumResolution="2445.98490512499"Extent="-15000000,2000000,-7000000,8000000"Background="#FFE3E3E3"MouseClick="QueryPoint_MouseClick"IsEnabled="False"><esri:ArcGISLocalTiledLayerID="BaseMap"Path="..\\Data\\TPKs\\Topographic.tpk"/><esri:ArcGISLocalDynamicMapServiceLayerPath="..\\Data\\MPKS\\USCitiesStates.mpk"/><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><ProgressBarx:Name="MyProgressBar"IsIndeterminate="True"VerticalAlignment="Bottom"Width="200"Height="20"Margin="10"Visibility="{Binding Path=IsBusy, Converter={StaticResource BooleanToVisibilityConverter}}"></ProgressBar></Grid></UserControl>
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Tasks;
using ESRI.ArcGIS.Client.Local;
using System.ComponentModel;
namespace ArcGISWPFSDK
{
publicpartialclass LocalIdentify : UserControl, INotifyPropertyChanged
{
publicevent PropertyChangedEventHandler PropertyChanged;
privatebool _isBusy = true;
publicbool IsBusy
{
get
{
return _isBusy;
}
set
{
_isBusy = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("IsBusy"));
}
}
}
private List<DataItem> _dataItems = null;
ESRI.ArcGIS.Client.Geometry.MapPoint _clickPoint;
LocalMapService _mapService;
public LocalIdentify()
{
InitializeComponent();
LocalMapService.GetServiceAsync("..\\Data\\MPKS\\USCitiesStates.mpk", (localMapService) =>
{
_mapService = localMapService;
MyMap.IsEnabled = true;
DataContext = this;
IsBusy = false;
});
}
privatevoid QueryPoint_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs e)
{
_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
};
IdentifyTask identifyTask = new IdentifyTask();
identifyTask.Url = _mapService.UrlMapService;
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);
}
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
Imports ESRI.ArcGIS.Client.Local
Imports System.ComponentModel
Namespace ArcGISWPFSDK
PartialPublicClass LocalIdentify
Inherits UserControl
Implements INotifyPropertyChanged
PublicEvent PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Private _isBusy AsBoolean = TruePublicProperty IsBusy() AsBooleanGetReturn _isBusy
EndGetSet(value AsBoolean)
_isBusy = value
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("IsBusy"))
EndSetEndPropertyPrivate _dataItems As List(Of DataItem) = NothingPrivate _clickPoint As ESRI.ArcGIS.Client.Geometry.MapPoint
Private _mapService As LocalMapService
PublicSubNew()
InitializeComponent()
LocalMapService.GetServiceAsync("..\Data\MPKS\USCitiesStates.mpk", Function(localMapService__1)
_mapService = localMapService__1
MyMap.IsEnabled = True
DataContext = Me
IsBusy = FalseEndFunction)
EndSubPrivateSub QueryPoint_MouseClick(sender AsObject, e As ESRI.ArcGIS.Client.Map.MouseEventArgs)
_clickPoint = 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 _
}
Dim identifyTask AsNew IdentifyTask()
identifyTask.Url = _mapService.UrlMapService
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
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