Click or drag to resize
Code Example - FindTask

Demonstrates using the Query.FindTask operation to search for text in attribute fields of an ArcGIS Server map service and display the features found as graphics in a GraphicsLayer.

Code Example
Find 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="FindTask.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 x:Name="LayoutRoot">

        <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 series of controls for the user to interact with to display results from a Query.FindTask operation. -->
            <StackPanel x:Name="StackPanel2" Orientation="Horizontal">
                <Button x:Name="Button1" Click="Button1_Click" Content="Find Cities"/>
                <TextBox x:Name="TextBox1" Text="newark" Width="100"  Margin="5,0,0,0"/>
                <CheckBox x:Name="CheckBox1" IsChecked="False" Content="Case Sensitive" Margin="5,0,0,0"/>
                <TextBlock x:Name="TextBlock2" Text="Number found: " Margin="15,0,0,0"/>
                <TextBox x:Name="TextBox2" Width="30"/>
                <Button x:Name="Button2" Click="Button2_Click" Content="Clear Graphics" Margin="100,0,0,0"/>
            </StackPanel>

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

                <!-- 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 an empty GraphicsLayer. -->
                    <esri:GraphicsLayer ID="myGraphicsLayer" />

                </esri:Map>
            </esri:MapView>
        </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 "FindTask.MainWindow" to be just "MainWindow".

namespace FindTask
{
    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, click the 'Find Cities' button to use the Query.FindTask to search for cities in the US " + 
            "that match what is entered in the TextBox. Click the 'Clear Graphics' button to reset for another Query.FindTask operation. Repeat the " + 
            "process again but this time check on the 'Case Sensitive' CheckBox before clicking the 'Find Cities' button, this time no result will be found.";

            // Set the initial extent of the Map to the continental US.
            Esri.ArcGISRuntime.Geometry.SpatialReference mySpatialReference = new Esri.ArcGISRuntime.Geometry.SpatialReference(102100);
            Esri.ArcGISRuntime.Geometry.Geometry myGeometry = new Esri.ArcGISRuntime.Geometry.Envelope(-14123689, 2232023, -7269960, 6839057, mySpatialReference);
            MapView1.Map.InitialViewpoint = new Esri.ArcGISRuntime.Controls.Viewpoint(myGeometry);
        }

        private async void Button1_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // Create a Uri that points to the public ESRI ArcGIS Server map service with the 'Find' operation supported.
            System.Uri myUri = new System.Uri("http://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer");

            // 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 from the TextBox. This is a mandatory Property that must be set!
            myFindParameters.SearchText = TextBox1.Text;

            // 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(0); // We will only search through the Cities layer.
            myFindParameters.LayerIDs = myLayerIds;

            // Allow the user to chose whether to have a case sensitive search or not.
            if (CheckBox1.IsChecked == true)
            {
                // We want a case sensitive (i.e. exact match) search.
                myFindParameters.Contains = false;
            }
            else
            {
                // We do not want a case sensitive (i.e. exact match) search. This is the default. Using this option
                // you can find records that only partially match the SearchText. Experiment by changing the SearchText
                // to a string like "new". This will find all cities that contain the letters 'new' in them, like:
                // New York, Newton, Pinewood, etc...
                myFindParameters.Contains = true;
            }

            // 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>();
            mySearchFields.Add("areaname");
            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;

            // Display the number of records that were found from the Query.FindTask search.
            TextBox2.Text = myFindItems.Count.ToString();

            // Get the GraphicsLayer that was defined in XAML.
            Esri.ArcGISRuntime.Layers.GraphicsLayer myGraphicsLayer = (Esri.ArcGISRuntime.Layers.GraphicsLayer)Map1.Layers["myGraphicsLayer"];

            // Loop through the records found.
            foreach (Esri.ArcGISRuntime.Tasks.Query.FindItem oneFindItem in myFindItems)
            {
                // Define a TextSymbol and to display the name of the city that is found.
                Esri.ArcGISRuntime.Symbology.TextSymbol myTextSymbol = new Esri.ArcGISRuntime.Symbology.TextSymbol();
                myTextSymbol.Text = oneFindItem.Value.ToString();
                myTextSymbol.Color = System.Windows.Media.Colors.Black;
                myTextSymbol.XOffset = 10;

                // Define a SimpleMarkerSymbol to display the exact location where the city is.
                Esri.ArcGISRuntime.Symbology.SimpleMarkerSymbol mySimpleMarkerSymbol = new Esri.ArcGISRuntime.Symbology.SimpleMarkerSymbol();
                mySimpleMarkerSymbol.Color = System.Windows.Media.Colors.Yellow;
                mySimpleMarkerSymbol.Size = 12;
                Esri.ArcGISRuntime.Symbology.SimpleLineSymbol mySimpleLineSymbol = new Esri.ArcGISRuntime.Symbology.SimpleLineSymbol();
                mySimpleLineSymbol.Color = System.Windows.Media.Colors.Black;
                mySimpleLineSymbol.Width = 1;
                mySimpleMarkerSymbol.Outline = mySimpleLineSymbol;

                // Create a CompositeSymbol to mash up the TextSymbol and SimpleMarkerSymbol.
                Esri.ArcGISRuntime.Symbology.CompositeSymbol myCompositeSymbol = new Esri.ArcGISRuntime.Symbology.CompositeSymbol();
                myCompositeSymbol.Symbols.Add(mySimpleMarkerSymbol);
                myCompositeSymbol.Symbols.Add(myTextSymbol);

                // Get the Feature (a Graphic) that is returned from the  Query.FindTask search, set it's Symbol, and
                // add it to the GraphicsLayer. This will cause an automatic redraw of the Map.
                Esri.ArcGISRuntime.Layers.Graphic myGraphic = new Esri.ArcGISRuntime.Layers.Graphic();
                myGraphic.Geometry = oneFindItem.Feature.Geometry;
                myGraphic.Symbol = myCompositeSymbol;
                myGraphicsLayer.Graphics.Add(myGraphic);
            }
        }

        private void Button2_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // Clear out the graphics in the GraphicsLayer.
            Esri.ArcGISRuntime.Layers.GraphicsLayer myGraphicsLayer = (Esri.ArcGISRuntime.Layers.GraphicsLayer)Map1.Layers["myGraphicsLayer"];
            myGraphicsLayer.Graphics.Clear();

            // Clear out the number of records found by the Query.FindTask operation.
            TextBox2.Text = "";
        }
    }
}