Click or drag to resize
Code Example - IdentifyParameter_DynamicLayerInfos

Demonstrates performing an Identify on a Dynamic Layer using the ESRI.ArcGISRuntime.Tasks.Query.IdentifyParameter.DynamicLayerInfos Property.

Code Example
Identify Parameter Dynamic Layer Infos

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="IdentifyParameter_DynamicLayerInfos.MainWindow"
        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/runtime/2013"
        Title="MainWindow" Height="600" Width="800">

    <Grid x:Name="LayoutRoot">

        <!-- Add a MapView Control. Note the use of the MouseDown and Loaded Events are used with code-behind logic.
  NOTE: it is helpful to specify the explicit dimensions (i.e. Height and Width) of the MapView so you can 
        use these properties in the code-behind without getting undefined values. -->
        <esri:MapView x:Name="MapView1" Background="White" HorizontalAlignment="Left" Margin="10,59,0,0"
                      VerticalAlignment="Top" Height="501" Width="772"
                      MouseDown="MapView1_MouseDown" Loaded="MapView1_Loaded">

            <!-- 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 Dynamic Layer based on an existing ArcMap and ArcGIS Server map service ArcGISDynamicMapServiceLayer 
                sub-layer (specifically the 'Counties' or [3] sub-layer). A Dynamic Layer is a web request where the client 
                application issues an ArcGIS Server command to generate an on-the-fly layer for display in the Map Control. 

                In order to create the Dynamic Layer the following are occurring: 
                (1) create a DynamicLayerInfo object and set the .ID and .Source Properties
                (2) add that DynamicLayerInfo object to the ArcGISDynamicMapServiceLayer .DynamicLayerInfos collection
                Note: It is not mandatory to set the ArcGISDynamicMapServiceLayer.LayerDrawingOptions if your Dynamic
                Layer is based upon a LayerMapSource. The Dynamic Layer that is getting created automatically uses the
                existing Symbology of the ArcGISDynamicMapServiceLayer sub-layer, unless you override it.
                -->
                <esri:ArcGISDynamicMapServiceLayer ID="myArcGISDynamicMapServiceLayer" Opacity="0.2" 
                                ServiceUri="http://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer" >
                    <esri:ArcGISDynamicMapServiceLayer.DynamicLayerInfos>
                        <esri:DynamicLayerInfoCollection>
                            <esri:DynamicLayerInfo ID="99">
                                <esri:DynamicLayerInfo.Source>
                                    <esri:LayerMapSource MapLayerID="3" />
                                </esri:DynamicLayerInfo.Source>
                            </esri:DynamicLayerInfo>
                        </esri:DynamicLayerInfoCollection>
                    </esri:ArcGISDynamicMapServiceLayer.DynamicLayerInfos>
                </esri:ArcGISDynamicMapServiceLayer>

                <!-- 
                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>
        <!-- Add a TextBlock to hold the instructions on how to use the sample code. -->
        <TextBlock Height="54" HorizontalAlignment="Left" Name="TextBlock1" VerticalAlignment="Top" Width="772" 
                   TextWrapping="Wrap" Margin="10,0,0,0" />

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

namespace IdentifyParameter_DynamicLayerInfos
{
    public partial class MainWindow : System.Windows.Window
    {

        // Create some Member (i.e Global in scope) variables for the ArGISDynamicMapServiceLayer, IdentifyParameters,
        // and IdentifyTask objects.
        private Esri.ArcGISRuntime.Layers.ArcGISDynamicMapServiceLayer _MyArcGISDynamicMapServiceLayer;
        private Esri.ArcGISRuntime.Tasks.Query.IdentifyParameters _MyIdentifyParameters;
        private Esri.ArcGISRuntime.Tasks.Query.IdentifyTask _MyIdentifyTask = new Esri.ArcGISRuntime.Tasks.Query.IdentifyTask(new System.Uri("http://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer"));

        public MainWindow()
        {
            InitializeComponent();

            // Add any initialization after the InitializeComponent() call.
            TextBlock1.Text = "After the Layers load in the Map, click on a 'County' to obtain Identify information for the " + 
                "Dynamic Layer. The Dynamic Layer that is generated is based upon the 'Workspace Type' of 'Database' using a " + 
                "LayerMapSource. The point is to show how the ESRI.ArcGISRuntime.Tasks.IdentifyParameter.DynamicLayerInfos Property " +
                "can be used to obtain Identify information about a Dynamic Layer.";

            // Zoom to Alaska.
            Esri.ArcGISRuntime.Geometry.Envelope myExtent;
            myExtent = new Esri.ArcGISRuntime.Geometry.Envelope(-20686124, 6677550, -13657836, 11597352);
            MapView1.Map.InitialViewpoint = new Esri.ArcGISRuntime.Controls.Viewpoint(myExtent);
        }

        private async void MapView1_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            // Generate the needed input parameter arguments for the IdentifyParameter class:
            // ------------------------------------------------------------------------------
            // (1) Get the real world coordinates of where the user clicked on the MapView
            System.Windows.Point myWindowsPoint = e.GetPosition(MapView1); // Get the location where the user clicked the mouse relative to the application
            Esri.ArcGISRuntime.Geometry.MapPoint myMapPoint = MapView1.ScreenToLocation(myWindowsPoint); // Now translate the screen coordinates to the real world Map coordinates

            // (2) Get the extent of the Map
            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 input parameter arguments just obtained.
            _MyIdentifyParameters = new Esri.ArcGISRuntime.Tasks.Query.IdentifyParameters(myMapPoint, myMapExtent, myTolerance, myHeight, myWidth);

            // Now set the other Parameters on the IdentifyTask request.
            _MyIdentifyParameters.ReturnGeometry = true; // Get the spatial geometry information.
            _MyIdentifyParameters.SpatialReference = MapView1.SpatialReference; // Get the SpatialReference information.
            _MyIdentifyParameters.DynamicLayerInfos = _MyArcGISDynamicMapServiceLayer.DynamicLayerInfos; // Get the Dynamic Layer information.
            _MyIdentifyParameters.LayerOption = Esri.ArcGISRuntime.Tasks.Query.LayerOption.Top;

            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 Graphics Layer defined in XAML and clear out any previous Graphics.
                Esri.ArcGISRuntime.Layers.GraphicsLayer myGraphicsLayer = (Esri.ArcGISRuntime.Layers.GraphicsLayer)(Map1.Layers["myGraphicsLayer"]);
                myGraphicsLayer.Graphics.Clear();

                // Create a StringBuilder object to hold Identify information that will be relayed to the user.
                System.Text.StringBuilder myStringBuilder = new System.Text.StringBuilder();

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

                if (myListOfIdentifyResult.Count == 1)
                {

                    // We are only interested in the first result of the Identify. 

                    // 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;

                    // Get the Feature (a Graphic) that is returned from the Identify operation, set it's Symbol, and
                    // add it to the GraphicsLayer. This will cause an automatic redraw of the Map.
                    Esri.ArcGISRuntime.Layers.Graphic myGraphic = (Esri.ArcGISRuntime.Layers.Graphic)myListOfIdentifyResult[0].Feature;
                    myGraphic.Symbol = newSimpleFillSymbol;
                    myGraphicsLayer.Graphics.Add(myGraphic);

                    // Get the DisplayFieldName and its Value of the first result.
                    string myDisplayFieldName = myListOfIdentifyResult[0].DisplayFieldName;
                    string myValue = myListOfIdentifyResult[0].Value.ToString();

                    // Add the Identify information into the StringBuilder and display it in a MessageBox.
                    myStringBuilder.Append(myDisplayFieldName + ": " + myValue);
                    System.Windows.MessageBox.Show(myStringBuilder.ToString());

                }
                else
                {
                    // Let the user know they did not click on a Feature.
                    System.Windows.MessageBox.Show("No Features Found!");
                }

            }
            catch (System.Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message);
            }
        }

        private void MapView1_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            // When the Map loads all of its Layers, get the ArcGISDynamicMapServiceLayer defined in XAML. Allow
            // DisableClientCaching for the IdentifyTask request.
            _MyArcGISDynamicMapServiceLayer = MapView1.Map.Layers["myArcGISDynamicMapServiceLayer"] as Esri.ArcGISRuntime.Layers.ArcGISDynamicMapServiceLayer;
        }

    }
}