Click or drag to resize
Code Example - GraphicsOverlay

Demonstrates using the Query.FindTask operation to return county features in an ArcGIS Server map service and display the features found as Graphics in a GraphicsOverlay. The user can perform a MapViewTapped on the Graphics to perform a GraphicsOverlay.HitTestAsync to select features and display attribute information.

Code Example
Graphics Overlay

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="GraphicsOverlay.MainWindow"
    Title="MainWindow" Height="600" Width="800"

    <Grid x:Name="LayoutRoot">

        <!-- Create a simple polygon fill renderer with a solid tan fill. The outline of the polygon is black. -->
            <esri:SimpleRenderer x:Key="mySimpleRenderer">
                    <esri:SimpleFillSymbol Color="BurlyWood"  Style="Solid">
                            <esri:SimpleLineSymbol Color="Black" Width="1" Style="Solid"/>

        <StackPanel x:Name="StackPanel1" Orientation="Vertical">

            <!-- Add a TextBlock to hold the instructions on how to use the sample code. -->
            <TextBlock Height="52" HorizontalAlignment="Left" Name="TextBlock1" VerticalAlignment="Top" Width="782" 
                   TextWrapping="Wrap" />

            <!-- Add a MapView. -->
            <esri:MapView x:Name="MapView1" Height="517" MapViewTapped="MapView1_MapViewTapped">

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

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


                <!-- Add a MapView.GraphicsOverlays collection. -->

                    <!-- Add a GraphicsOverlay to hold Graphics added via code behind from a FindTask operation. Set the Renderer to draw the polygon graphics. -->
                    <esri:GraphicsOverlay Renderer="{StaticResource mySimpleRenderer}"/>





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

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

            // Add the instructions on how to use this example code to the TextBlock.
            TextBlock1.Text = "When the application loads, a Query.FindTask will occur to obtain the Features for counties in Minnesota. " + 
                "Graphics will be added to a GraphicsOverlay which is displayed on top of the Map. Tap on a county and it will be be selected " + 
                "and attributes for the Graphic will be displayed in a Message Box.";

            // Set the initial extent of the Map to the Minnesota.
            Esri.ArcGISRuntime.Geometry.SpatialReference mySpatialReference = new Esri.ArcGISRuntime.Geometry.SpatialReference(102100);
            Esri.ArcGISRuntime.Geometry.Geometry myGeometry = new Esri.ArcGISRuntime.Geometry.Envelope(-11260915, 5266673, -9622263, 6368165, mySpatialReference);
            MapView1.Map.InitialViewpoint = new Esri.ArcGISRuntime.Controls.Viewpoint(myGeometry);

            // Call an asynchronous subroutine to initialize obtaining Features from a map service via a Query.FindTask and display the Features
            // in a GraphicsOverlay.

        public async void Init()
            // Create a Uri that points to the public ESRI ArcGIS Server map service with the 'Find' operation supported.
            System.Uri myUri = new System.Uri("");

            // Create a new FindParamaters which hold the instructions of how the Query.FindTask will operate.
            Esri.ArcGISRuntime.Tasks.Query.FindParameters myFindParameters = new Esri.ArcGISRuntime.Tasks.Query.FindParameters();

            // Set the FindParameters text to search for. This is a mandatory Property that must be set!
            myFindParameters.SearchText = "Minnesota";

            // Define the layers in the ArcGIS Server map service for which the Query.FindTask will execute on. This is a mandatory Property that must be set!
            System.Collections.Generic.List<int> myLayerIds = new System.Collections.Generic.List<int>();
            myLayerIds.Add(3); // We will find records in the Counties layer.
            myFindParameters.LayerIDs = myLayerIds;

            // Define the attribute fields in the layer for which the we will attempt to find the SearchText. If the FindParameters.SearchFields
            // is not specified, then all of the attributes fields in the layer will be searched for the SearchText.
            System.Collections.Generic.List<string> mySearchFields = new System.Collections.Generic.List<string>();
            myFindParameters.SearchFields = mySearchFields;

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

            // Perform the Query.FindTask search. 
            Esri.ArcGISRuntime.Tasks.Query.FindResult myFindResult = await myFindTask.ExecuteAsync(myFindParameters);

            // Get the results from the FindResult object.
            System.Collections.Generic.IReadOnlyList<Esri.ArcGISRuntime.Tasks.Query.FindItem> myFindItems = myFindResult.Results;

            // Get the GraphicsOverlay by its index position in the GraphicsOverlays collection.
            Esri.ArcGISRuntime.Controls.GraphicsOverlay myGraphicsOverlay = MapView1.GraphicsOverlays[0];

            // Loop through the records found.
            foreach (Esri.ArcGISRuntime.Tasks.Query.FindItem oneFindItem in myFindItems)
                // Create a new Graphic based on the returned results of the  Query.FindTask search. 
                Esri.ArcGISRuntime.Layers.Graphic myGraphic = new Esri.ArcGISRuntime.Layers.Graphic();
                myGraphic.Geometry = oneFindItem.Feature.Geometry;

                // Get the attributes (i.e. field name and value) for each FindItem (aka. record).
                System.Collections.Generic.IDictionary<string, object> myAttributes = oneFindItem.Feature.Attributes;

                // Add all of the attributes from the Feature to the Graphic.
                foreach (var oneAttribute in myAttributes)
                    myGraphic.Attributes.Add(oneAttribute.Key, oneAttribute.Value);

                // Add the Graphic to the GraphicsOverlay.

                // Note: The symbology of Graphics in the GraphicsOverlay was defined in the XAML.

        private async void MapView1_MapViewTapped(object sender, Esri.ArcGISRuntime.Controls.MapViewInputEventArgs e)
            // Get the GraphicsOverlay by its index position in the GraphicsOverlays collection.
            Esri.ArcGISRuntime.Controls.GraphicsOverlay myGraphicsOverlay = MapView1.GraphicsOverlays[0];

            // Clear out any prior selected Graphics in the GraphicsOverlay.

            // Get a single Graphic from the GraphicsOverlay based upon a user mouse-click/tap on the MapView.
            Esri.ArcGISRuntime.Layers.Graphic myGraphic = await myGraphicsOverlay.HitTestAsync(MapView1, e.Position);

            // Only select a Graphic and display it's attribute information if we have a valid HitTestAsync result.
            if (myGraphic != null)
                // Select the Graphic in the GraphicsOverlay.
                myGraphic.IsSelected = true;

                // Get the attributes (i.e. field name and value) that were added to the Graphic in the Init subroutine.
                System.Collections.Generic.IDictionary<string, object> myAttributes = myGraphic.Attributes;

                // Create a StringBuilder to hold the attribute field names and values to display to the user.
                System.Text.StringBuilder myStringBuilder = new System.Text.StringBuilder();

                // Add all of the attribute field names and values to the StringBuider.
                foreach (var oneAttribute in myAttributes)
                    myStringBuilder.AppendLine(oneAttribute.Key.ToString() + ": " + oneAttribute.Value.ToString());

                // Display the attributes to the user for the Graphic they selected.