Click or drag to resize
Code Example - IdentifyTask

Demonstrates using the Query.IdentifyTask operation find a feature based upon it's geographic position in an ArcGIS Server map service and display it as a graphic in a GraphicsLayer.

Code Example
Identify Task

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.

XAML
<Window x:Class="IdentifyTask.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="600" Width="800"
    xmlns:esri="http://schemas.esri.com/arcgis/runtime/2013">

    <Grid>

        <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>
            <StackPanel Orientation="Horizontal">

                <!-- A MapView Control to display various GIS layers. Make use of the MapViewTapped Event to capture when the user clicks on the Map. -->
                <esri:MapView x:Name="MapView1" Width="600" Height="480" VerticalAlignment="Top" Margin="2,2,2,2" MapViewTapped="MapView1_MapViewTapped">

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

                        <!-- Add a backdrop ArcGISTiledMapServiceLayer. -->
                        <esri:ArcGISTiledMapServiceLayer ID="myArcGISTiledMapServiceLayer" 
                              ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer" />

                        <!-- Add a backdrop ArcGISDynamicMapServiceLayer which shows various features for which the user will want to do an Identify on. -->
                        <esri:ArcGISDynamicMapServiceLayer ID="myArcGISDynamicMapServiceLayer"  
                                ServiceUri="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer" />

                        <!-- Add an empty GraphicsLayer to showcase where the user clicks on the Map for the feature being Identified. -->
                        <esri:GraphicsLayer ID="myGraphicsLayer" />

                    </esri:Map>

                </esri:MapView>

                <!-- Display the returned feature attribute values that were found by the IdentifyTask. -->
                <StackPanel Orientation="Vertical">
                    <Label Content="IdentifyTask results:"/>
                    <TextBlock x:Name="TextBlockFeatureAttributes" Width="187" Height="456" Background="LightGoldenrodYellow"/>
                </StackPanel>

            </StackPanel>
        </StackPanel>
    </Grid>

</Window>

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 "IdentifyTask.MainWindow" to be just "MainWindow".

namespace IdentifyTask
{
    public partial class MainWindow : System.Windows.Window
    {
        public MainWindow()
        {
            InitializeComponent();

            // Add the instructions on how to use this example code to the TextBlock.
            TextBlock1.Text = "When the application loads, tap on the map over a County to perform an Query.IdentifyTask operation. If exactly one " + 
                "county is found, a few attribute values for that record will be displayed in the TextBlock.";

            // Create a Geometry based on an Envelope set to the Great Lakes region. 
            Esri.ArcGISRuntime.Geometry.Geometry myGeometry = new Esri.ArcGISRuntime.Geometry.Envelope(-10100156, 4932149, -8847812, 5934024);

            // 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 MapView1_MapViewTapped(object sender, Esri.ArcGISRuntime.Controls.MapViewInputEventArgs e)
        {
            // Create a Uri that points to the public ESRI ArcGIS Server map service with the 'Identify' operation supported.
            System.Uri myUri = new System.Uri("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer");

            // Create a new Query.IdentifyTask using the ArcGIS Server map service. 
            Esri.ArcGISRuntime.Tasks.Query.IdentifyTask myIdentifyTask = new Esri.ArcGISRuntime.Tasks.Query.IdentifyTask(myUri);

            // Generate the needed input parameter arguments for the IdentifyParameter class:
            // ------------------------------------------------------------------------------
            // (1) Get the real world coordinates of where the user clicked on the MapView.
            Esri.ArcGISRuntime.Geometry.MapPoint myMapPoint = e.Location;

            // (2) Get the extent of the MapView.
            Esri.ArcGISRuntime.Geometry.Envelope myMapExtent = MapView1.Extent;

            // (3) The screen pixel tolerance size for performing the identify.
            System.Int32 myTolerance = 1;

            // (4) The height of the MapView Control.
            System.Int32 myHeight = System.Convert.ToInt32(MapView1.Height);

            // (5) The width of the MapView Control.
            System.Int32 myWidth = System.Convert.ToInt32(MapView1.Width);

            // Create a new IdentifyParameters object using the five input parameters arguments just obtained.
            Esri.ArcGISRuntime.Tasks.Query.IdentifyParameters myIdentifyParameters = new Esri.ArcGISRuntime.Tasks.Query.IdentifyParameters(myMapPoint, myMapExtent, myTolerance, myHeight, myWidth);

            // Now set the other Parameters on the IdentifyTask request:
            // ---------------------------------------------------------
            // Get the spatial geometry information.
            myIdentifyParameters.ReturnGeometry = true;

            // Perform the Identify operation on the Counties layer (i.e. Layer ID = 3) in the ArcGIS Server map service.
            myIdentifyParameters.LayerIDs.Add(3);

            // Set the SpatialReference information. 
            // NOTE: If the MapView.SpatialReference DOES NOT EQUAL the Query.IdenfityTask ArcGIS Server map service SpatialReference then no records will be found.
            // Hence setting this property is always a good idea to ensure that you get results back!
            myIdentifyParameters.SpatialReference = MapView1.SpatialReference;

            try
            {
                // Execute the Asynchronous request to Identify the Dynamic Layer Features where the user clicked on the Map.
                Esri.ArcGISRuntime.Tasks.Query.IdentifyResult myIdentifyResult = await myIdentifyTask.ExecuteAsync(myIdentifyParameters);

                // Get the GraphicsLayer defined in XAML and clear out any previous Graphics.
                Esri.ArcGISRuntime.Layers.GraphicsLayer myGraphicsLayer = (Esri.ArcGISRuntime.Layers.GraphicsLayer)(Map1.Layers["myGraphicsLayer"]);
                myGraphicsLayer.Graphics.Clear();

                // Get the IdentifyResults.
                System.Collections.Generic.IReadOnlyList<Esri.ArcGISRuntime.Tasks.Query.IdentifyItem> myListOfIdentifyResult = myIdentifyResult.Results;

                // For this example, we are only interested in the first result of the Identify. 
                if (myListOfIdentifyResult.Count == 1)
                {
                    // Get the first IdentifyItem from the ReadOnlyList.
                    Esri.ArcGISRuntime.Tasks.Query.IdentifyItem myIdentifyItem = myListOfIdentifyResult[0];

                    // Get the Feature from the IdentifyItem. 
                    Esri.ArcGISRuntime.Data.Feature myFeature = myIdentifyItem.Feature;

                    // Get the desired Field attribute values from the GeodatabaseFeature.
                    System.Collections.Generic.IDictionary<string, object> myAttributes = myFeature.Attributes;
                    var myCountyName = myAttributes["NAME"];
                    var myStateName = myAttributes["STATE_NAME"];                    
                    var myPOP2000 = myAttributes["POP2000"];
                    var myObjectID = myAttributes["ObjectID"];

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

                    // Display the StringBuilder text in the TextBlock.
                    TextBlockFeatureAttributes.Text = myStringBuilder.ToString();

                    // Create a SimpleFillSymbol using the solid Yellow brush and set the Border to be Red with a thickness 
                    // of 2. This will be used to accentuate the display of the Feature the user clicked on.
                    Esri.ArcGISRuntime.Symbology.SimpleFillSymbol newSimpleFillSymbol = new Esri.ArcGISRuntime.Symbology.SimpleFillSymbol();
                    newSimpleFillSymbol.Color = System.Windows.Media.Colors.Yellow;
                    Esri.ArcGISRuntime.Symbology.SimpleLineSymbol newSimpleLineSymbol = new Esri.ArcGISRuntime.Symbology.SimpleLineSymbol();
                    newSimpleLineSymbol.Color = System.Windows.Media.Colors.Red;
                    newSimpleLineSymbol.Width = 2;
                    newSimpleLineSymbol.Style = Esri.ArcGISRuntime.Symbology.SimpleLineStyle.Solid;
                    newSimpleFillSymbol.Outline = newSimpleLineSymbol;
                    newSimpleFillSymbol.Style = Esri.ArcGISRuntime.Symbology.SimpleFillStyle.Solid;

                    // Create a Graphic based upon the geometry of the Feature then set it's Symbol, and add it to the GraphicsLayer.
                    // NOTE: This will cause an automatic redraw of the Map.
                    Esri.ArcGISRuntime.Layers.Graphic myGraphic = new Esri.ArcGISRuntime.Layers.Graphic();
                    myGraphic.Geometry = myFeature.Geometry;
                    myGraphic.Symbol = newSimpleFillSymbol;
                    myGraphicsLayer.Graphics.Add(myGraphic);
                }
                else
                {
                    // Let the user know they did not click on a Feature.
                    System.Windows.MessageBox.Show("No Features found or possibly more that one if you clicked on the boundary of multiple features.");
                }
            }
            catch (System.Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message);
            }
        }
    }
}