Click or drag to resize
Code Example - FeatureLayer_Identify

Demonstrates performing and Identify on a FeatureLayer. The FeatureLayer.HitTestAsync method is used to select (and highlight) the feature a user clicks/taps on as well as display specific attributes for that feature.

Code Example
Feature Layer Identify

This section contains selected code files from a Visual Studio project that emphasize specific ArcGIS Runtime SDK (Windows Desktop) features. For example: some code examples may accomplish the bulk of the work as a configuration property in the .xaml file and hence only the .xaml file will be shown in detail below. In other code examples, the .xaml is used to define the configuration of graphical elements for the application but the application logic is performed in the code behind, hence you may see both the .xaml and .cs/.vb files shown in detail below.

<Window x:Class="FeatureLayer_Identify.MainWindow"
    Title="MainWindow" Height="600" Width="800"


        <StackPanel Orientation="Vertical">
            <StackPanel Orientation="Vertical">

                <!-- TextBlock to provide the instructions on how to use the sample code. It will be 
                populated with instructions in the code-behind when the application loads. -->
                <TextBlock Height="58" HorizontalAlignment="Left" Name="TextBlock1" VerticalAlignment="Top" 
                           Width="770" TextWrapping="Wrap"  Margin="10,2,0,0" />

            <StackPanel Orientation="Horizontal">

                <!-- Add a button to allow the user to select features in the FeatureLayer. -->
                <Button Content="Click me to perform an Identify on a single feature in the FeatureLayer" Margin="3,3" Width="785"
                        x:Name="ButtonDrawRectangle" Click="ButtonDrawRectangle_Click"/>

            <StackPanel Orientation="Horizontal">

                <!-- A MapView Control to display various GIS layers. -->
                <esri:MapView x:Name="MapView1" Width="600" Height="480" VerticalAlignment="Top" Margin="2,2,2,2">

                    <!-- A Map. -->
                    <esri:Map  x:Name="Map1" >

                        <!-- Add a backdrop ArcGISTiledMapServiceLayer-->
                        <esri:ArcGISTiledMapServiceLayer ID="myArcGISTiledMapServiceLayer" 
                              ServiceUri="" />

                <!-- Display the FeatureLayer feature attribute values that were selected by the identify. -->
                <StackPanel Orientation="Vertical">
                    <Label Content="Selected Feature Attributes:"/>
                    <TextBlock x:Name="TextBlockFeatureAttributes" Width="187" Height="456"/>



SPECIAL NOTE: The XAML displayed above comes from a C# project. If you are a VB.NET developer, you will need to modify the text for the x:Class namespace from "FeatureLayer_Identify.MainWindow" to be just "MainWindow".

namespace FeatureLayer_Identify
    public partial class MainWindow : System.Windows.Window
        public MainWindow()

            // Provide instructions on how to use the sample. 
            TextBlock1.Text = "When the application loads a FeatureLayer will automatically be added to the Map (it was specified in XAML). Click the Button to " + 
                              "tap/click a MapPoint over a feature to select it. The feature that is selected will highlight and a few attribute field values will be displayed in the " + 
                              "TextBlock. Clicking the button again will reset the feature selection process.";

            // Define the Uri to the ArcGIS Server web service that hosts a FeatureLayer.
            System.Uri myUri = new System.Uri("");

            // Create a new FeatureLayer from the Uri and provide an ID for it.
            Esri.ArcGISRuntime.Layers.FeatureLayer myFeatureLayer = new Esri.ArcGISRuntime.Layers.FeatureLayer(myUri);
            myFeatureLayer.ID = "US_States";

            // Create an outline SimpleLineSymbol for the polygon FeatureLayer.
            Esri.ArcGISRuntime.Symbology.SimpleLineSymbol myOutline = new Esri.ArcGISRuntime.Symbology.SimpleLineSymbol();
            myOutline.Color = System.Windows.Media.Colors.Black;
            myOutline.Style = Esri.ArcGISRuntime.Symbology.SimpleLineStyle.Solid;
            myOutline.Width = 1;

            // Create a SimpleFillSymbol for the polygon FeatureLayer.
            Esri.ArcGISRuntime.Symbology.SimpleFillSymbol mySimpleFillSymbol = new Esri.ArcGISRuntime.Symbology.SimpleFillSymbol();
            mySimpleFillSymbol.Color = System.Windows.Media.Colors.LightGreen;
            mySimpleFillSymbol.Style = Esri.ArcGISRuntime.Symbology.SimpleFillStyle.Solid;
            mySimpleFillSymbol.Outline = myOutline;

            // Create a SimpleRenderer from the SimpleFillSymbol.
            Esri.ArcGISRuntime.Symbology.SimpleRenderer mySimpleRenderer = new Esri.ArcGISRuntime.Symbology.SimpleRenderer();
            mySimpleRenderer.Symbol = mySimpleFillSymbol;

            // Set the Renderer of the FeatureLayer to out custom SimpleRenderer.
            myFeatureLayer.Renderer = mySimpleRenderer;

            // Get the ServiceFeatureTable from the FeatureLayer and make sure that all attribute fields can be returned from a query. 
            Esri.ArcGISRuntime.Data.ServiceFeatureTable myServiceFeatureTable = (Esri.ArcGISRuntime.Data.ServiceFeatureTable)myFeatureLayer.FeatureTable;
            myServiceFeatureTable.OutFields = Esri.ArcGISRuntime.Tasks.Query.OutFields.All;

            // NOTE: If you do not want to return all of the attributes Fields in the FeatureLayer.FeatureTable, you can add the Fields individually
            // by doing code similar to this:
            //Esri.ArcGISRuntime.Tasks.Query.OutFields myOutFields = new Esri.ArcGISRuntime.Tasks.Query.OutFields();
            //myServiceFeatureTable.OutFields = myOutFields;

            // HELPER FACT:
            // To know specific fields names that are part of the FeatureLayer, enter the Uri for the FeatureLayer in an Internet Browser and look at the 
            // Fields section in the REST documentation.

            // Add the FeatureLayer to the LayerCollection of the Map.

            // Create a Geometry based on an Envelope set to a particular extent of North-East US.
            Esri.ArcGISRuntime.Geometry.Geometry myGeometry = new Esri.ArcGISRuntime.Geometry.Envelope(-9453251, 4249734, -7115541, 6119901);

            // Set the initial extent of the Map using the InitialViewpoint Property to that of our Geometry.
            MapView1.Map.InitialViewpoint = new Esri.ArcGISRuntime.Controls.Viewpoint(myGeometry);

        private async void ButtonDrawRectangle_Click(object sender, System.Windows.RoutedEventArgs e)
                // Get the FeatureLayer from the Map.
                Esri.ArcGISRuntime.Controls.Map myMap = MapView1.Map;
                Esri.ArcGISRuntime.Layers.LayerCollection myLayerCollection = myMap.Layers;
                Esri.ArcGISRuntime.Layers.FeatureLayer myFeatureLayer = (Esri.ArcGISRuntime.Layers.FeatureLayer)myLayerCollection[1];

                // Clear out any existing selected features in the FeatureLayer and the attribute Field values in the TextBlock.
                TextBlockFeatureAttributes.Text = "";

                // Get the Editor associated with the MapView. The Editor enables drawing and editing graphic objects.
                Esri.ArcGISRuntime.Controls.Editor myEditor = MapView1.Editor;

                // Get the MapPoint that the user tapped/clicked on the Map. Execution of the code stops here until the user is done drawing the MapPoint. 
                Esri.ArcGISRuntime.Geometry.MapPoint myPoint = await myEditor.RequestPointAsync();

                if (myPoint != null)
                    // Translate the MapPoint into Microsoft Point object.
                    System.Windows.Point myWindowsPoint = MapView1.LocationToScreen(myPoint);

                    // Get the FeatureTable from the FeatureLayer.
                    Esri.ArcGISRuntime.Data.FeatureTable myFeatureTable = myFeatureLayer.FeatureTable;

                    // Use the FeatureLayer.HitTestAsync Method to retrieve the feature (actually the FeatureLayer ID) that intersects the point that was
                    // clicked/tapped on the Map by the user. By leaving off the optional last parameter, the HitTestAsync return a single record; this is 
                    // perfect for doing an Identify type of operation.
                    long[] myFeatureLayerRowIDs = await myFeatureLayer.HitTestAsync(MapView1, myWindowsPoint);

                    if (myFeatureLayerRowIDs.Length == 1)

                        // Cause the features in the FeatureLayer to highlight (cyan) in the Map.

                        // Perform a Query on the FeatureLayer.FeatureTable using the ObjectID of the feature tapped/clicked on in the map. Actually it is a List of ObjectID values 
                        // but since we are trying for an Identify type of operation we really only have one ObjectID in the list.
                        System.Collections.Generic.IEnumerable<Esri.ArcGISRuntime.Data.GeodatabaseFeature> myGeoDatabaseFeature = (System.Collections.Generic.IEnumerable<Esri.ArcGISRuntime.Data.GeodatabaseFeature>)await myFeatureTable.QueryAsync(myFeatureLayerRowIDs);

                        foreach (Esri.ArcGISRuntime.Data.GeodatabaseFeature oneGeoDatabaseFeature in myGeoDatabaseFeature)

                            // Get the desired Field attribute values from the GeodatabaseFeature.
                            System.Collections.Generic.IDictionary<string, object> myAttributes = oneGeoDatabaseFeature.Attributes;
                            string myStateName = myAttributes["STATE_NAME"].ToString();
                            int myPOP2000 = (int)myAttributes["POP2000"];
                            object myObjectID = myAttributes["ObjectID"];

                            // Construct a StringBuilder to hold the text from the Field attributes.
                            System.Text.StringBuilder myStringBuilder = new System.Text.StringBuilder();
                            myStringBuilder.AppendLine("State Name: " + myStateName);
                            myStringBuilder.AppendLine("Population in 2000: " + myPOP2000.ToString());
                            myStringBuilder.AppendLine("ObjectID: " + myObjectID.ToString());

                            // Display the StringBuilder text in the TextBlock.
                            TextBlockFeatureAttributes.Text = myStringBuilder.ToString();
            catch (System.Threading.Tasks.TaskCanceledException)
                // This exception occurred because the user has already clicked the button but has not clicked/tapped a MapPoint on the Map yet.
                System.Windows.MessageBox.Show("Click or tap on over a feature in the map to select it.");
            catch (System.Exception ex)
                // We had some kind of issue. Display to the user so it can be corrected.