Identify task
With the Identify task, you can search the layers in a map for features that intersect an input geometry. Once the matching features are returned, you can use .NET code to display their geometries and attributes in your Windows Phone application. To use an Identify task, you will need to include code to define the task's user interface and specify its execution logic.
A sample of an Identify task application is available in the Identify sample in the Query section of the Interactive SDK.
Creating an Identify task
The following sections of this topic walk you through building an example of XAML and .NET code (in this case C#) for a simple Windows Phone application that includes an Identify task. It includes the following steps:
- Creating an input interface for the Identify task
- Creating an output interface for the Identify task
- Implementing the Identify task's execution logic, including:
This application defines an Identify task that uses the Map control's Hold gesture 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 ListBox.
The following sections assume you have created a Windows Phone application with a map as a base layer as described in Creating a map. The XAML view of your application's ContentGrid in MainPage.xaml should look like the following code:
<Grid x:Name="ContentGrid" Grid.Row="1">
<esri:Map x:Name="MyMap" Extent="-120, 20, -100, 40">
<esri:Map.Layers>
<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer"
Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer">
</esri:ArcGISTiledMapServiceLayer>
</esri:Map.Layers>
</esri:Map>
</Grid>
The code in your code-behind file, MainPage.xaml.cs, should be unchanged from when you created your Windows Phone project in Visual Studio.
Creating an input interface for the Find 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 after the Map's definition, specify a Border to use as the background for the task's instructions. The border's resizing behavior will allow you to also use it as the background for the list of results and their attributes.
<Border x:Name="IdentifyBorder" Background="#99000000" BorderThickness="1" CornerRadius="5" HorizontalAlignment="Center" BorderBrush="Gray" VerticalAlignment="Top" Margin="5"> <Grid x:Name="IdentifyGrid" HorizontalAlignment="Right" VerticalAlignment="Top" > <Grid.RowDefinitions> <RowDefinition Height="50" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> </Grid> </Border>
- Define a TextBlock to inform the user about how to execute the Identify task.
<Border x:Name="IdentifyBorder" Background="#99000000" BorderThickness="1" CornerRadius="5" HorizontalAlignment="Center" BorderBrush="Gray" VerticalAlignment="Top" Margin="5"> <Grid x:Name="IdentifyGrid" HorizontalAlignment="Right" VerticalAlignment="Top" > <Grid.RowDefinitions> <RowDefinition Height="50" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> </Grid> <TextBlock Grid.Row="0" Text="Tap and hold on the map to identify a feature" Foreground="White" FontSize="20" Margin="10,5,10,5" /> </Border>
- To use a point as the input geometry for the Identify task, the Map's Hold gesture can be used. The gesture event fires whenever the Map control is touched. Define the MapGesture attribute within the Map's XAML element as shown below.
<esri:Map x:Name="MyMap" Extent="-130, 20, -60, 40" MapGesture="MyMap_MapGesture" > <esri:Map.Layers> <esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer" Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"> </esri:ArcGISTiledMapServiceLayer> </esri:Map.Layers> </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 SimpleMarkerSymbol, which is included in the ESRI.ArcGIS.Client.Symbols namespace of the ESRI.ArcGIS.Client assembly. To use a SimpleMarkerSymbol in XAML, first add an XML namespace reference to the ESRI.ArcGIS.Client.Symbols namespace.
xmlns:esriSymbols="clr-namespace:ESRI.ArcGIS.Client.Symbols;assembly=ESRI.ArcGIS.Client"
Note:
You will need to add a reference to the System.Runtime.Serialization assembly to your project to work with symbols.
- Now declare a SimpleMarkerSymbol as a resource of the root Grid element.
<Grid.Resources> <esriSymbols:SimpleMarkerSymbol /> </Grid.Resources>
- Specify the "x:Key" attribute for the symbol. This will allow you to reference the symbol from the page's code-behind.
<Grid.Resources> <esriSymbols:SimpleMarkerSymbol x:Key="IdentifyLocationSymbol" /> </Grid.Resources>
- Provide visual details so that the symbol is a small blue circle.
<Grid.Resources> <esriSymbols:SimpleMarkerSymbol x:Name="IdentifyLocationSymbol" Color="Blue" Size="15" /> </Grid.Resources>
- Add a GraphicsLayer to the Map control to use for drawing the symbol on the map.
<esri:Map x:Name="MyMap" Extent="-130, 20, -60, 40" MapGesture="MyMap_MapGesture" > <esri:Map.Layers> <esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer" Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"> </esri:ArcGISTiledMapServiceLayer> <esri:GraphicsLayer ID="IdentifyIconGraphicsLayer" /> </esri:Map.Layers> </esri:Map>
- There are now instructions displayed so that the user of the application will know how to execute the identify task.
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, along with a ListBox to show the selected feature's attributes.
- The Border element you defined earlier as the background for the Identify task's instructions will also be used as the background for the ComboBox and ListBox that display results. Add a Grid to the Border's container element to position the results information and control their visibility. Within the Grid 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
TextBlock are not visible.
<Border x:Name="IdentifyBorder" Background="#99000000" BorderThickness="1" CornerRadius="5" HorizontalAlignment="Center" BorderBrush="Gray" VerticalAlignment="Top" Margin="5"> <Grid x:Name="IdentifyGrid" HorizontalAlignment="Right" VerticalAlignment="Top" > <Grid.RowDefinitions> <RowDefinition Height="50" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> </Grid> <TextBlock Grid.Row="0" Text="Tap and hold on the map to identify a feature" Foreground="White" FontSize="20" Margin="10,5,10,5" /> <Grid Grid.Row="1" x:Name="IdentifyResultsPanel" Margin="5,1,5,5" HorizontalAlignment="Center" VerticalAlignment="Top" Visibility="Collapsed" > </Grid> </Border>
- In the results, a ComboBox displaying the result features will display, along with a ListBox of the attributes of the feature currently selected in the ComboBox. These elements may be rather large and cover the small available phone screen space. To allow the user to clear the results, a close button will also be provided on the panel. To organize all three of these controls in the IdentifyResultsPanel Grid, define some columns and rows.
<Grid Grid.Row="1" x:Name="IdentifyResultsPanel" Margin="5,1,5,5" HorizontalAlignment="Center" VerticalAlignment="Top" Visibility="Collapsed" > <Grid.RowDefinitions> <RowDefinition Height="50" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="50" /> </Grid.ColumnDefinitions> </Grid>
- Define the ComboBox for selecting result features inside the IdentifyResultsPanel element, 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 TextBlock and the result feature drawn on the map. Place the ComboBox in the first column and row of the results panel.
<Grid Grid.Row="1" x:Name="IdentifyResultsPanel" Margin="5,1,5,5" HorizontalAlignment="Center" VerticalAlignment="Top" Visibility="Collapsed" > <Grid.RowDefinitions> <RowDefinition Height="50" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="50" /> </Grid.ColumnDefinitions> <ComboBox x:Name="IdentifyComboBox" MinWidth="150" Grid.Row="0" Grid.Column="0" SelectionChanged="IdentifyComboBox_SelectionChanged" Margin="5,1,5,5" Foreground="Black" > </ComboBox> </Grid>
- Place a close button in the first row and second column of the results Grid. Declare a handler for its Click event, which you will define in a later step.
<Grid Grid.Row="1" x:Name="IdentifyResultsPanel" Margin="5,1,5,5" HorizontalAlignment="Center" VerticalAlignment="Top" Visibility="Collapsed" > <Grid.RowDefinitions> <RowDefinition Height="50" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="50" /> </Grid.ColumnDefinitions> <ComboBox x:Name="IdentifyComboBox" MinWidth="150" Grid.Row="0" Grid.Column="0" SelectionChanged="IdentifyComboBox_SelectionChanged" Margin="5,1,5,5" Foreground="Black" > </ComboBox> <Button x:Name="CloseResults" Content="X" Background="DarkRed" Grid.Row="0" Grid.Column="1" Margin="-7" Padding="0" Click="CloseResults_Click" /> </Grid>
- In the next row of the results panel, and spanning both of its columns, include a ListBox to host the attributes of the identify result selected in the ComboBox. In the ArcGIS API for Windows Phone, 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 Silverlight 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.
<Grid Grid.Row="1" x:Name="IdentifyResultsPanel" Margin="5,1,5,5" HorizontalAlignment="Center" VerticalAlignment="Top" Visibility="Collapsed" > <Grid.RowDefinitions> <RowDefinition Height="50" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="50" /> </Grid.ColumnDefinitions> <ComboBox x:Name="IdentifyComboBox" MinWidth="150" Grid.Row="0" Grid.Column="0" SelectionChanged="IdentifyComboBox_SelectionChanged" Margin="5,1,5,5" Foreground="Black" > </ComboBox> <ListBox x:Name="DataListBox" Grid.Row="1" Grid.ColumnSpan="2"> <ListBox.ItemTemplate> <DataTemplate> <Grid Width="400"> <Grid.ColumnDefinitions> <ColumnDefinition Width="200" /> <ColumnDefinition Width="200" /> </Grid.ColumnDefinitions> <TextBlock Width="200" Text="{Binding Key}" Grid.Column="0" FontSize="20" /> <TextBlock Width="200" Text="{Binding Value}" Grid.Column="1" TextWrapping="Wrap" FontSize="20" /> </Grid> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid>
- The output interface is now ready to display the identify results.
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 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 Windows Phone application's main page (e.g. MainPage.xaml.cs). In this example, C# is used.
Executing the task
In the application's XAML, you declared the MyMap_MapGesture method as a handler for the Map's gesture events. 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 Windows Phone development, like Silverlight, XAML is reserved for an application's presentation layer, while the code-behind is where business logic is implemented.
- Including using statements for the ESRI.ArcGIS.Client and ESRI.ArcGIS.Client.Tasks namespaces.
using ESRI.ArcGIS.Client; using ESRI.ArcGIS.Client.Tasks;
- Declare variables in the MainPage class to use in the Identify operation, including the IdentifyTask and the IdentifyParameters:
IdentifyTask identifyTask; IdentifyParameters identifyParameters;
- In the MainPage constructor, 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.
public MainPage() { InitializeComponent(); 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.
public MainPage() { InitializeComponent(); 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.
public MainPage() { InitializeComponent(); 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.
public MainPage() { InitializeComponent(); identifyTask = new IdentifyTask("http://sampleserver1.arcgisonline.com/ArcGIS/" + "rest/services/Demographics/ESRI_Census_USA/MapServer"); identifyTask.ExecuteCompleted += IdentifyTask_ExecuteCompleted; identifyTask.Failed += IdentifyTask_Failed; 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.
public MainPage() { InitializeComponent(); identifyTask = new IdentifyTask("http://sampleserver1.arcgisonline.com/ArcGIS/" + "rest/services/Demographics/ESRI_Census_USA/MapServer"); identifyTask.ExecuteCompleted += IdentifyTask_ExecuteCompleted; identifyTask.Failed += IdentifyTask_Failed; identifyParameters = new IdentifyParameters(); identifyParameters.LayerOption = LayerOption.visible; }
- Declare the MyMap_MapGesture method.
private void MyMap_MapGesture(object sender, ESRI.ArcGIS.Client.Map.MapGestureEventArgs e) { }
- Listen for Hold events, which will trigger Identify tasks.
private void MyMap_MapGesture(object sender, ESRI.ArcGIS.Client.Map.MapGestureEventArgs e) { if (e.Gesture == ESRI.ArcGIS.Client.GestureType.Hold) { } }
- Retrieve the GraphicsLayer for the Identify icon and clear it of any previously drawn symbols.
private void MyMap_MapGesture(object sender, ESRI.ArcGIS.Client.Map.MapGestureEventArgs e) { if (e.Gesture == ESRI.ArcGIS.Client.GestureType.Hold) { GraphicsLayer graphicsLayer = MyMap.Layers["IdentifyIconGraphicsLayer"] as GraphicsLayer; graphicsLayer.ClearGraphics(); } }
- Instantiate a new Graphic. Set its geometry to be the point held on the map and its symbol to be the SimpleMarkerSymbol resource that references the Identify icon.
private void MyMap_MapGesture(object sender, ESRI.ArcGIS.Client.Map.MapGestureEventArgs e) { if (e.Gesture == ESRI.ArcGIS.Client.GestureType.Hold) { GraphicsLayer graphicsLayer = MyMap.Layers["IdentifyIconGraphicsLayer"] as GraphicsLayer; graphicsLayer.ClearGraphics(); ESRI.ArcGIS.Client.Graphic graphic = new ESRI.ArcGIS.Client.Graphic() { Geometry = e.MapPoint, Symbol = IdentifyLocationSymbol }; } }
- Add the Identify graphic to the GraphicsLayer.
private void MyMap_MapGesture(object sender, ESRI.ArcGIS.Client.Map.MapGestureEventArgs e) { if (e.Gesture == ESRI.ArcGIS.Client.GestureType.Hold) { GraphicsLayer graphicsLayer = MyMap.Layers["IdentifyIconGraphicsLayer"] as GraphicsLayer; graphicsLayer.ClearGraphics(); ESRI.ArcGIS.Client.Graphic graphic = new ESRI.ArcGIS.Client.Graphic() { Geometry = e.MapPoint, Symbol = IdentifyLocationSymbol }; graphicsLayer.Graphics.Add(graphic); } }
- Use the Map control's properties to initialize the map extent, width, and height of the Identify parameters.
private void MyMap_MapGesture(object sender, ESRI.ArcGIS.Client.Map.MapGestureEventArgs e) { if (e.Gesture == ESRI.ArcGIS.Client.GestureType.Hold) { GraphicsLayer graphicsLayer = MyMap.Layers["IdentifyIconGraphicsLayer"] as GraphicsLayer; graphicsLayer.ClearGraphics(); ESRI.ArcGIS.Client.Graphic graphic = new ESRI.ArcGIS.Client.Graphic() { Geometry = e.MapPoint, Symbol = IdentifyLocationSymbol }; graphicsLayer.Graphics.Add(graphic); 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_MapGesture(object sender, ESRI.ArcGIS.Client.Map.MapGestureEventArgs e) { if (e.Gesture == ESRI.ArcGIS.Client.GestureType.Hold) { GraphicsLayer graphicsLayer = MyMap.Layers["IdentifyIconGraphicsLayer"] as GraphicsLayer; graphicsLayer.ClearGraphics(); ESRI.ArcGIS.Client.Graphic graphic = new ESRI.ArcGIS.Client.Graphic() { Geometry = e.MapPoint, Symbol = IdentifyLocationSymbol }; graphicsLayer.Graphics.Add(graphic); Parameters.MapExtent = MyMap.Extent; identifyParameters.Width = (int)MyMap.ActualWidth; identifyParameters.Height = (int)MyMap.ActualHeight; identifyParameters.Geometry = e.MapPoint; } }
- Execute the Identify task.
private void MyMap_MapGesture(object sender, ESRI.ArcGIS.Client.Map.MapGestureEventArgs e) { if (e.Gesture == ESRI.ArcGIS.Client.GestureType.Hold) { GraphicsLayer graphicsLayer = MyMap.Layers["IdentifyIconGraphicsLayer"] as GraphicsLayer; graphicsLayer.ClearGraphics(); ESRI.ArcGIS.Client.Graphic graphic = new ESRI.ArcGIS.Client.Graphic() { Geometry = e.MapPoint, Symbol = IdentifyLocationSymbol }; graphicsLayer.Graphics.Add(graphic); Parameters.MapExtent = MyMap.Extent; identifyParameters.Width = (int)MyMap.ActualWidth; identifyParameters.Height = (int)MyMap.ActualHeight; identifyParameters.Geometry = e.MapPoint; identifyTask.ExecuteAsync(identifyParameters); } }
Displaying results
In the MainPage class' constructor, 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 ListBox to display the selected feature's attributes, and a button to close the results information. On the ComboBox, you specified the IdentifyComboBox_SelectionChanged method as the handler for the ComboBox's SelectionChanged event. On the button, you specified the CloseResults_Click method as the handler for the button's Click 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 ListBox. Finally you will implement the Click handler to close the identify results display.
- 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 Grid containing the Identify ComboBox and results ListBox visible.
private void IdentifyTask_ExecuteCompleted(object sender, IdentifyEventArgs args) { IdentifyComboBox.Items.Clear(); if (args.IdentifyResults.Count > 0) { IdentifyResultsPanel.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) { IdentifyResultsPanel.Visibility = Visibility.Visible; foreach (IdentifyResult result in args.IdentifyResults) { string title = string.Format("{0} ({1})", result.Value.ToString(), result.LayerName); ComboBoxItem cbi = new ComboBoxItem(); cbi.Background = new SolidColorBrush(Colors.Black); cbi.Foreground = new SolidColorBrush(Colors.White); cbi.Content = title; IdentifyComboBox.Items.Add(cbi); } 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
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) { IdentifyResultsPanel.Visibility = Visibility.Visible; foreach (IdentifyResult result in args.IdentifyResults) { string title = string.Format("{0} ({1})", result.Value.ToString(), result.LayerName); ComboBoxItem cbi = new ComboBoxItem(); cbi.Background = new SolidColorBrush(Colors.Black); cbi.Foreground = new SolidColorBrush(Colors.White); cbi.Content = title; IdentifyComboBox.Items.Add(cbi); } 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 ListBox.
private void IdentifyTask_ExecuteCompleted(object sender, IdentifyEventArgs args) { IdentifyComboBox.Items.Clear(); if (args.IdentifyResults.Count > 0) { IdentifyResultsPanel.Visibility = Visibility.Visible; foreach (IdentifyResult result in args.IdentifyResults) { string title = string.Format("{0} ({1})", result.Value.ToString(), result.LayerName); ComboBoxItem cbi = new ComboBoxItem(); cbi.Background = new SolidColorBrush(Colors.Black); cbi.Foreground = new SolidColorBrush(Colors.White); cbi.Content = title; IdentifyComboBox.Items.Add(cbi); } IdentifyComboBox.UpdateLayout(); _lastIdentifyResult = args.IdentifyResults; IdentifyComboBox.SelectedIndex = 0; } else { } }
- If no features were found, hide the Grid containing the Identify ComboBox and ListBox. Notify the user with a MessageBox.
private void IdentifyTask_ExecuteCompleted(object sender, IdentifyEventArgs args) { IdentifyComboBox.Items.Clear(); if (args.IdentifyResults.Count > 0) { IdentifyResultsPanel.Visibility = Visibility.Visible; foreach (IdentifyResult result in args.IdentifyResults) { string title = string.Format("{0} ({1})", result.Value.ToString(), result.LayerName); ComboBoxItem cbi = new ComboBoxItem(); cbi.Background = new SolidColorBrush(Colors.Black); cbi.Foreground = new SolidColorBrush(Colors.White); cbi.Content = title; IdentifyComboBox.Items.Add(cbi); } IdentifyComboBox.UpdateLayout(); _lastIdentifyResult = args.IdentifyResults; IdentifyComboBox.SelectedIndex = 0; } else { IdentifyResultsPanel.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 (e.g. the ComboBox is cleared).
void IdentifyComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { }
- 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) { 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) { if (IdentifyComboBox.SelectedIndex > -1) { Graphic selectedFeature = _lastIdentifyResult[IdentifyComboBox.SelectedIndex].Feature; } }
- Update the Identify ListBox to show the attributes of the selected feature. In the page's XAML, recall that you specified a Grid two columns in the ListBox, each containing a TextBox, and that the TextBoxes are 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 ListBox to the selected Graphic (i.e. feature) by passing this attributes Dictionary to the ListBox's ItemsSource property.
void IdentifyComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (IdentifyComboBox.SelectedIndex > -1) { Graphic selectedFeature = _lastIdentifyResult[IdentifyComboBox.SelectedIndex].Feature; DataListBox.ItemsSource = selectedFeature.Attributes; } }
- Implement the Click event for the button to close the identify results display.
private void CloseResults_Click(object sender, RoutedEventArgs e) { }
- Close the identify results display by setting its visibility to collapsed.
private void CloseResults_Click(object sender, RoutedEventArgs e) { IdentifyResultsPanel.Visibility = Visibility.Collapsed; }
- Clear the graphic marking the point held on the map.
private void CloseResults_Click(object sender, RoutedEventArgs e) { IdentifyResultsPanel.Visibility = Visibility.Collapsed; GraphicsLayer graphicsLayer = MyMap.Layers["IdentifyIconGraphicsLayer"] as GraphicsLayer; graphicsLayer.ClearGraphics(); }
Handling execution errors
The final step left in implementing the Identify task is to handle when the task is executed and fails. In the MainPage class' constructor, you specified IdentifyTask_ExecuteCompleted as the handler for the task's ExecuteCompleted event. This event receives the Identify task's failure information, which can be passed to the application's user.
- 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); }