Finding Features in the Map

Performing a find operation to search the map for features is a key aspect of a geographical information system. The Find task allows you to search one or more layers in a map for features with attribute values that match or contain an input value. Once the matching features are returned, you can use .NET code to display their geometries and attributes in your WPF application. To use a Find task, you will need to include code to define its user interface and specify its execution logic. In the ArcGIS Runtime SDK for WPF the Find task allows you to search local or online services.

User Interface

The Find task does not specify the user interface, therefore implementation is required to allow the users to define the user input and display the geometries and attributes of the results in the WPF Application. The best practice is to separate an application's presentation layer from its business logic. In WPF, XAML is intended to house the presentation layer, while the code-behind is for an application's business logic. There are numerous ways in which the user input to the operation can be provided through the interface of your WPF application. It might be by typing a search term in a textbox or selecting a search term from a listbox. Results for this operation could be displayed by adding graphics to the map in a graphics layer, or by populating a listbox or datagrid.

Using the Find task with local data

To execute a Find task, you need to instantiate the task, specify the layer that will be queried, wire the task's event handlers, initialize the task's search parameters, and call the task's execution method.

To initialize the Find task in code, you are required to pass the URL of the LocalMapService or layer that the task will use to the task's constructor. Here's an example of initializing a Find task:

Create a new ESRI.ArcGIS.Client.Local.LocalMapService with the path to the data source

LocalMapService.GetServiceAsync("Path to datasource", ms =>
{  
{
});

Declare and instantiate the ESRI.ArcGIS.Client.Tasks.FindTask class. Set the map service that the task will use to lookup addresses by passing the local map service's URL to the task's constructor.

LocalMapService.GetServiceAsync("Path to datasource", ms =>
{ 
FindTask findTask = new FindTask(ms.UrlMapService);  
});

Using the Find task with Online Data

To use the Find task with online data you will be required to pass the URL of an online service or layer. To find the URL, you can use the ArcGIS Services Directory. An example is below:

FindTask findTask = new FindTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +
		"Demographics/ESRI_Census_USA/MapServer/");

Input Parameters

Before using one of the Find task's operations, you will need to define the operation's input parameters. These parameters will vary depending on the operation. The FindParameters object is used to define the execution parameters for Find tasks. Below is an example of initializing the parameters for the Find task:

Declare and instantiate a FindParameters object.

FindParameters findParameters = new FindParameters();

On the FindParameters object, set the layer to be searched by adding a layer ID to the LayerIds property.

findParameters.LayerIds.AddRange(new int[] { 0 });

Specify the field to be searched in the find operation.

findParameters.SearchFields.AddRange(new string[] { "AREANAME" });

If you want to draw the Find task's results on the map, specify that geometry be returned with the results.

findParameters.ReturnGeometry = true;

Specify the field to be searched in the find operation.

findParameters.SearchFields.AddRange(new string[] { "AREANAME" });

If you want to draw the Find task's results on the map, specify that geometry be returned with the results.

findParameters.ReturnGeometry = true;

Define the value to search for. It could be the text from a WPF UserControl such as a TextBox.

findParameters.SearchText = FindTextBox.Text;

Execute the find task with the parameters specified by the FindParameters member variable.

findTask.ExecuteAsync(findParameters);

Results

The Find task has a property named FindResult that contains the most recently generated set of results. Each FindResult contains the feature found, the name and ID of the layer containing the feature, the name of the field containing the matching value, and other information. This property is useful for using WPF data binding to automatically update results displays whenever a task has executed or for storing the set of results for later reference.

1/27/2015