
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 Silverlight application. To use a Find task, you will need to include code to define its user interface and specify its execution logic.
An example of XAML and .NET code (in this case C#) for a simple Silverlight application that includes a Find task is shown below. This application defines a Find task that uses a TextBox control for specifying the input value and a button for executing the task. Result features are displayed in a GraphicsLayer that has MapTips enabled. The rest of this document will walk you through how the Find task is defined in the example.
[XAML]
<UserControl x:Class="SilverlightApp.Page" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:esri="clr-namespace:ESRI.ArcGIS.Client;assembly=ESRI.ArcGIS.Client" xmlns:esriConverters="clr-namespace:ESRI.ArcGIS.Client.ValueConverters;assembly=ESRI.ArcGIS.Client" xmlns:esriSymbols="clr-namespace:ESRI.ArcGIS.Client.Symbols;assembly=ESRI.ArcGIS.Client"> <Grid x:Name="LayoutRoot" Background="White"> <!-- FIND TASK OUTPUT RESOURCES --> <Grid.Resources> <esriConverters:DictionaryConverter x:Name="MyDictionaryConverter" /> <esriSymbols:SimpleFillSymbol x:Name="ResultsFillSymbol" Fill="#64FF0000" BorderBrush="Red" BorderThickness="2" /> </Grid.Resources> <!-- MAP --> <esri:Map x:Name="MyMap" Extent="-125, 25, -65, 55" > <esri:Map.Layers> <esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer" Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/> <esri:GraphicsLayer ID="MyGraphicsLayer"> <esri:GraphicsLayer.MapTip> <Grid Background="LightYellow"> <StackPanel Margin="5"> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Converter={StaticResource MyDictionaryConverter}, ConverterParameter=NAME, Mode=OneWay}" FontWeight="Bold" /> <TextBlock Text=" County, " FontWeight="Bold" /> <TextBlock Text="{Binding Converter={StaticResource MyDictionaryConverter}, ConverterParameter=STATE_NAME, Mode=OneWay}" FontWeight="Bold" /> </StackPanel> <StackPanel Orientation="Horizontal"> <TextBlock Text="Population (2007): " /> <TextBlock Text="{Binding Converter={StaticResource MyDictionaryConverter}, ConverterParameter=POP2007, Mode=OneWay}" /> </StackPanel> </StackPanel> <Border BorderBrush="Black" BorderThickness="1" /> </Grid> </esri:GraphicsLayer.MapTip> </esri:GraphicsLayer> </esri:Map.Layers> </esri:Map> <!-- FIND TASK INTERFACE --> <Canvas HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,15,7,0" Width="230" > <Rectangle Fill="#CC5C90B2" Stroke="Gray" RadiusX="10" RadiusY="10" Width="210" Height="55" /> <TextBlock Text="Find counties with names containing:" Foreground="White" FontSize="10" Margin="10,5,0,0" /> <TextBox x:Name="FindTextBox" Width="150" Margin="15,22,0,0" Text="Wash" /> <Button x:Name="FindButton" Content="Find" Margin="168,23,0,0" Click="FindButton_Click" /> </Canvas> </Grid> </UserControl>
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Tasks;
using ESRI.ArcGIS.Client.Symbols;
using ESRI.ArcGIS.Client.ValueConverters;
namespace SilverlightApp
{
public partial class Page : UserControl
{
public Page() { InitializeComponent(); }
// Execute task when find button is clicked
private void FindButton_Click(object sender, RoutedEventArgs e)
{
// Find task initialization
FindTask findTask = new FindTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +
"Demographics/ESRI_Census_USA/MapServer/");
findTask.ExecuteCompleted += FindTask_ExecuteCompleted;
findTask.Failed += FindTask_Failed;
// Initialize find parameters. Specify NAME field in counties layer as search field
// and returning of feature geometry with find results.
FindParameters findParameters = new FindParameters();
findParameters.LayerIds.AddRange(new int[] { 3 });
findParameters.SearchFields.AddRange(new string[] { "NAME" });
findParameters.ReturnGeometry = true;
// Use textbox text as value to search for.
findParameters.SearchText = FindTextBox.Text;
findTask.ExecuteAsync(findParameters);
}
// Draw results when find is complete
private void FindTask_ExecuteCompleted(object sender, FindEventArgs args)
{
// Clear previous results
GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
graphicsLayer.ClearGraphics();
// Check for new results
if (args.FindResults.Count > 0)
{
// Add results to map
foreach (FindResult result in args.FindResults)
{
result.Feature.Symbol = ResultsFillSymbol;
graphicsLayer.Graphics.Add(result.Feature);
}
}
else
{
MessageBox.Show("No features found");
}
}
// Notify when find fails
private void FindTask_Failed(object sender, TaskFailedEventArgs args)
{
MessageBox.Show("Find failed: " + args.Error);
}
}
}
The following steps assume you have created a Silverlight application with a map and a base layer as described in Creating a Map. The XAML view of your application's main page (e.g. Page.xaml) should look similar to the following:
<UserControl x:Class="SilverlightApp.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:esri="clr-namespace:ESRI.ArcGIS.Client;assembly=ESRI.ArcGIS.Client">
<Grid x:Name="LayoutRoot" Background="White">
<!-- MAP -->
<esri:Map x:Name="MyMap" Extent="-125, 25, -65, 55" >
<esri:Map.Layers>
<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer"
Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
</esri:Map.Layers>
</esri:Map>
</Grid>
</UserControl>
The code in the main page's code-behind (e.g. Page.xaml.cs) should be unchanged from when you created your Silverlight application project in Visual Studio.
Since tasks do not define user interfaces, you need to implement an interface for the Find task's input to allow your application's users to execute find operations. For this, the example simply includes a TextBox for defining the input value and a Button to execute the task.
<Canvas HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,15,7,0" Width="230" > </Canvas>
<Canvas HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,15,7,0" Width="230" >
<Rectangle Fill="#CC5C90B2" Stroke="Gray" RadiusX="10" RadiusY="10" Width="210" Height="55" />
</Canvas><Canvas HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,15,7,0" Width="230" >
<Rectangle Fill="#CC5C90B2" Stroke="Gray" RadiusX="10" RadiusY="10" Width="210" Height="55" />
<TextBlock Text="Find counties with names containing:" Foreground="White" FontSize="10" Margin="10,5,0,0" />
</Canvas>
<Canvas HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,15,7,0" Width="230" >
<Rectangle Fill="#CC5C90B2" Stroke="Gray" RadiusX="10" RadiusY="10" Width="210" Height="55" />
<TextBlock Text="Find counties with names containing:" Foreground="White" FontSize="10" Margin="10,5,0,0" />
<TextBox x:Name="FindTextBox" Width="150" Margin="15,22,0,0" />
</Canvas>
<Canvas HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,15,7,0" Width="230" >
<Rectangle Fill="#CC5C90B2" Stroke="Gray" RadiusX="10" RadiusY="10" Width="210" Height="55" />
<TextBlock Text="Find counties with names containing:" Foreground="White" FontSize="10" Margin="10,5,0,0" />
<TextBox x:Name="FindTextBox" Width="150" Margin="15,22,0,0" Text="Wash" />
</Canvas>
<Canvas HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,15,7,0" Width="230" >
<Rectangle Fill="#CC5C90B2" Stroke="Gray" RadiusX="10" RadiusY="10" Width="210" Height="55" />
<TextBlock Text="Find counties with names containing:" Foreground="White" FontSize="10" Margin="10,5,0,0" />
<TextBox x:Name="FindTextBox" Width="150" Margin="15,22,0,0" Text="Wash" />
<Button x:Name="FindButton" Content="Find" Margin="168,23,0,0" />
</Canvas>
<Canvas HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,15,7,0" Width="230" >
<Rectangle Fill="#CC5C90B2" Stroke="Gray" RadiusX="10" RadiusY="10" Width="210" Height="55" />
<TextBlock Text="Find counties with names containing:" Foreground="White" FontSize="10" Margin="10,5,0,0" />
<TextBox x:Name="FindTextBox" Width="150" Margin="15,22,0,0" Text="Wash" />
<Button x:Name="FindButton" Content="Find" Margin="168,23,0,0" Click="FindButton_Click" />
</Canvas>To display the results of the Find task, you need to specify an output interface. For displaying the geometry of result features, you will define a GraphicsLayer within the Map element and a SimpleFillSymbol as a static resource. Then you will specify MapTips on the GraphicsLayer for displaying the features' attributes. The MapTips will use the DictionaryConverter class to enable binding to attributes on the Find task's results.
<UserControl x:Class="SilverlightApp.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:esri="clr-namespace:ESRI.ArcGIS.Client;assembly=ESRI.ArcGIS.Client"
xmlns:esriConverters="clr-namespace:ESRI.ArcGIS.Client.ValueConverters;assembly=ESRI.ArcGIS.Client"
xmlns:esriSymbols="clr-namespace:ESRI.ArcGIS.Client.Symbols;assembly=ESRI.ArcGIS.Client"><Grid.Resources> <esriConverters:DictionaryConverter x:Name="MyDictionaryConverter" /> </Grid.Resources>
<Grid.Resources>
<esriConverters:DictionaryConverter x:Name="MyDictionaryConverter" />
<esriSymbols:SimpleFillSymbol x:Name="ResultsFillSymbol" Fill="#64FF0000" BorderBrush="Red"
BorderThickness="2" />
</Grid.Resources><esri:Map x:Name="MyMap" Extent="-125, 25, -65, 55" >
<esri:Map.Layers>
<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer"
Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
<esri:GraphicsLayer ID="MyGraphicsLayer">
</esri:GraphicsLayer>
</esri:Map.Layers>
</esri:Map>
<esri:Map x:Name="MyMap" Extent="-125, 25, -65, 55" >
<esri:Map.Layers>
<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer"
Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
<esri:GraphicsLayer ID="MyGraphicsLayer">
<esri:GraphicsLayer.MapTip>
<Grid Background="LightYellow">
<StackPanel Margin="5">
</StackPanel>
<Border BorderBrush="Black" BorderThickness="1" />
</Grid>
</esri:GraphicsLayer.MapTip>
</esri:GraphicsLayer>
</esri:Map.Layers>
</esri:Map>
<esri:Map x:Name="MyMap" Extent="-125, 25, -65, 55" >
<esri:Map.Layers>
<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer"
Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
<esri:GraphicsLayer ID="MyGraphicsLayer">
<esri:GraphicsLayer.MapTip>
<Grid Background="LightYellow">
<StackPanel Margin="5">
<StackPanel Orientation="Horizontal">
</StackPanel>
</StackPanel>
<Border BorderBrush="Black" BorderThickness="1" />
</Grid>
</esri:GraphicsLayer.MapTip>
</esri:GraphicsLayer>
</esri:Map.Layers>
</esri:Map>
<esri:Map x:Name="MyMap" Extent="-125, 25, -65, 55" >
<esri:Map.Layers>
<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer"
Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
<esri:GraphicsLayer ID="MyGraphicsLayer">
<esri:GraphicsLayer.MapTip>
<Grid Background="LightYellow">
<StackPanel Margin="5">
<StackPanel Orientation="Horizontal">
<TextBlock FontWeight="Bold" />
</StackPanel>
</StackPanel>
<Border BorderBrush="Black" BorderThickness="1" />
</Grid>
</esri:GraphicsLayer.MapTip>
</esri:GraphicsLayer>
</esri:Map.Layers>
</esri:Map>
<esri:Map x:Name="MyMap" Extent="-125, 25, -65, 55" >
<esri:Map.Layers>
<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer"
Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
<esri:GraphicsLayer ID="MyGraphicsLayer">
<esri:GraphicsLayer.MapTip>
<Grid Background="LightYellow">
<StackPanel Margin="5">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Converter={StaticResource MyDictionaryConverter},
ConverterParameter=NAME, Mode=OneWay}" FontWeight="Bold" />
</StackPanel>
</StackPanel>
<Border BorderBrush="Black" BorderThickness="1" />
</Grid>
</esri:GraphicsLayer.MapTip>
</esri:GraphicsLayer>
</esri:Map.Layers>
</esri:Map>
<esri:Map x:Name="MyMap" Extent="-125, 25, -65, 55" >
<esri:Map.Layers>
<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer"
Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
<esri:GraphicsLayer ID="MyGraphicsLayer">
<esri:GraphicsLayer.MapTip>
<Grid Background="LightYellow">
<StackPanel Margin="5">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Converter={StaticResource MyDictionaryConverter},
ConverterParameter=NAME, Mode=OneWay}" FontWeight="Bold" />
<TextBlock Text=" County, " FontWeight="Bold" />
</StackPanel>
</StackPanel>
<Border BorderBrush="Black" BorderThickness="1" />
</Grid>
</esri:GraphicsLayer.MapTip>
</esri:GraphicsLayer>
</esri:Map.Layers>
</esri:Map>
<esri:Map x:Name="MyMap" Extent="-125, 25, -65, 55" >
<esri:Map.Layers>
<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer"
Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
<esri:GraphicsLayer ID="MyGraphicsLayer">
<esri:GraphicsLayer.MapTip>
<Grid Background="LightYellow">
<StackPanel Margin="5">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Converter={StaticResource MyDictionaryConverter},
ConverterParameter=NAME, Mode=OneWay}" FontWeight="Bold" />
<TextBlock Text=" County, " FontWeight="Bold" />
<TextBlock Text="{Binding Converter={StaticResource MyDictionaryConverter},
ConverterParameter=STATE_NAME, Mode=OneWay}" FontWeight="Bold" />
</StackPanel>
</StackPanel>
<Border BorderBrush="Black" BorderThickness="1" />
</Grid>
</esri:GraphicsLayer.MapTip>
</esri:GraphicsLayer>
</esri:Map.Layers>
</esri:Map><esri:Map x:Name="MyMap" Extent="-125, 25, -65, 55" >
<esri:Map.Layers>
<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer"
Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
<esri:GraphicsLayer ID="MyGraphicsLayer">
<esri:GraphicsLayer.MapTip>
<Grid Background="LightYellow">
<StackPanel Margin="5">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Converter={StaticResource MyDictionaryConverter},
ConverterParameter=NAME, Mode=OneWay}" FontWeight="Bold" />
<TextBlock Text=" County, " FontWeight="Bold" />
<TextBlock Text="{Binding Converter={StaticResource MyDictionaryConverter},
ConverterParameter=STATE_NAME, Mode=OneWay}" FontWeight="Bold" />
</StackPanel>
<StackPanel Orientation="Horizontal">
</StackPanel>
</StackPanel>
<Border BorderBrush="Black" BorderThickness="1" />
</Grid>
</esri:GraphicsLayer.MapTip>
</esri:GraphicsLayer>
</esri:Map.Layers>
</esri:Map><esri:Map x:Name="MyMap" Extent="-125, 25, -65, 55" >
<esri:Map.Layers>
<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer"
Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
<esri:GraphicsLayer ID="MyGraphicsLayer">
<esri:GraphicsLayer.MapTip>
<Grid Background="LightYellow">
<StackPanel Margin="5">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Converter={StaticResource MyDictionaryConverter},
ConverterParameter=NAME, Mode=OneWay}" FontWeight="Bold" />
<TextBlock Text=" County, " FontWeight="Bold" />
<TextBlock Text="{Binding Converter={StaticResource MyDictionaryConverter},
ConverterParameter=STATE_NAME, Mode=OneWay}" FontWeight="Bold" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Population (2007): " />
</StackPanel>
</StackPanel>
<Border BorderBrush="Black" BorderThickness="1" />
</Grid>
</esri:GraphicsLayer.MapTip>
</esri:GraphicsLayer>
</esri:Map.Layers>
</esri:Map>
<esri:Map x:Name="MyMap" Extent="-125, 25, -65, 55" >
<esri:Map.Layers>
<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer"
Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
<esri:GraphicsLayer ID="MyGraphicsLayer">
<esri:GraphicsLayer.MapTip>
<Grid Background="LightYellow">
<StackPanel Margin="5">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Converter={StaticResource MyDictionaryConverter},
ConverterParameter=NAME, Mode=OneWay}" FontWeight="Bold" />
<TextBlock Text=" County, " FontWeight="Bold" />
<TextBlock Text="{Binding Converter={StaticResource MyDictionaryConverter},
ConverterParameter=STATE_NAME, Mode=OneWay}" FontWeight="Bold" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Population (2007): " />
<TextBlock Text="{Binding Converter={StaticResource MyDictionaryConverter},
ConverterParameter=POP2007, Mode=OneWay}" />
</StackPanel>
</StackPanel>
<Border BorderBrush="Black" BorderThickness="1" />
</Grid>
</esri:GraphicsLayer.MapTip>
</esri:GraphicsLayer>
</esri:Map.Layers>
</esri:Map>Now that you've specified the Find task's user interface, you need to define its execution logic. This execution logic can be divided into three parts:
You will implement these components in .NET code contained in the main page's code-behind. This code is linked to the XAML presentation layer by manipulating elements that you declared in XAML with "x:Name" or "ID" attributes and implementing methods that you declared in XAML as event handlers. The steps below assume that you are adding code to the Page class in the code-behind file for your Silverlight application's main page (e.g. Page.xaml.cs). In this example, C# is used.
Executing the task
To execute a Find task, you need to instantiate the task, specifying the map service that will be searched, wire the task's event handlers, initialize the task's search parameters, and call the task's execution method. The steps below will show you how to do this in the code-behind of your application's main page (e.g. Page.xaml.cs). The task is declared and initialized in the code-behind because tasks alone do not define any user interface, but rather encapsulate pieces of execution logic. In Silverlight, XAML is reserved for an application's presentation layer, while the code-behind is where business logic is implemented.
The code shown in these steps is written in C#.
- In the code-behind class of your application's main page, implement a handler for the FindButton's click event. Recall that you declared this handler when you defined the FindButton control in the page's XAML.
private void FindButton_Click(object sender, RoutedEventArgs e) { }
- In the click handler, declare and instantiate a Find task. Set the map service layer that the task will search by passing the layer's URL to the Find task's constructor. To find the URL, you can use the ArcGIS Services Directory. See the Discovering Services topic for more information. This example uses the states layer of the ESRI_Census_USA service.
private void FindButton_Click(object sender, RoutedEventArgs e) { FindTask findTask = new FindTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" + "Demographics/ESRI_Census_USA/MapServer/"); }
- Specify a handler for the task's ExecuteCompleted event. The method specified will be called when the Find task is done executing. You will implement this handler in the "Displaying results" section.
private void FindButton_Click(object sender, RoutedEventArgs e) { FindTask findTask = new FindTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" + "Demographics/ESRI_Census_USA/MapServer/"); findTask.ExecuteCompleted += FindTask_ExecuteCompleted; }
- Specify a handler for the task's Failed event, which fires when there is a problem executing the task. You will define this handler in the "Handling execution errors" section.
private void FindButton_Click(object sender, RoutedEventArgs e) { FindTask findTask = new FindTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" + "Demographics/ESRI_Census_USA/MapServer/"); findTask.ExecuteCompleted += FindTask_ExecuteCompleted; findTask.Failed += FindTask_Failed; }
- Declare and instantiate a FindParameters object. The FindParameters object is used to define the execution parameters for Find tasks.
private void FindButton_Click(object sender, RoutedEventArgs e) { FindTask findTask = new FindTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" + "Demographics/ESRI_Census_USA/MapServer/"); findTask.ExecuteCompleted += FindTask_ExecuteCompleted; findTask.Failed += FindTask_Failed; FindParameters findParameters = new FindParameters(); }
- On the FindParameters object, set the layer to be searched by adding a layer ID of 3 to the LayerIds property. This corresponds to the counties layer.
private void FindButton_Click(object sender, RoutedEventArgs e) { FindTask findTask = new FindTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" + "Demographics/ESRI_Census_USA/MapServer/"); findTask.ExecuteCompleted += FindTask_ExecuteCompleted; findTask.Failed += FindTask_Failed; FindParameters findParameters = new FindParameters(); findParameters.LayerIds.AddRange(new int[] { 3 }); }
- Specify that the NAME field be searched in the find operation.
private void FindButton_Click(object sender, RoutedEventArgs e) { FindTask findTask = new FindTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" + "Demographics/ESRI_Census_USA/MapServer/"); findTask.ExecuteCompleted += FindTask_ExecuteCompleted; findTask.Failed += FindTask_Failed; FindParameters findParameters = new FindParameters(); findParameters.LayerIds.AddRange(new int[] { 3 }); findParameters.SearchFields.AddRange(new string[] { "NAME" }); }
- Since you will draw the Find task's results on the map, specify that geometry be returned with the results.
private void FindButton_Click(object sender, RoutedEventArgs e) { FindTask findTask = new FindTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" + "Demographics/ESRI_Census_USA/MapServer/"); findTask.ExecuteCompleted += FindTask_ExecuteCompleted; findTask.Failed += FindTask_Failed; FindParameters findParameters = new FindParameters(); findParameters.LayerIds.AddRange(new int[] { 3 }); findParameters.SearchFields.AddRange(new string[] { "NAME" }); findParameters.ReturnGeometry = true; }
- Define the value to search for as the text in the FindTextBox control.
private void FindButton_Click(object sender, RoutedEventArgs e) { FindTask findTask = new FindTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" + "Demographics/ESRI_Census_USA/MapServer/"); findTask.ExecuteCompleted += FindTask_ExecuteCompleted; findTask.Failed += FindTask_Failed; FindParameters findParameters = new FindParameters(); findParameters.LayerIds.AddRange(new int[] { 3 }); findParameters.SearchFields.AddRange(new string[] { "NAME" }); findParameters.ReturnGeometry = true; findParameters.SearchText = FindTextBox.Text; }
- Execute the find task with the paramters specified by the FindParameters member variable.
private void FindButton_Click(object sender, RoutedEventArgs e) { FindTask findTask = new FindTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" + "Demographics/ESRI_Census_USA/MapServer/"); findTask.ExecuteCompleted += FindTask_ExecuteCompleted; findTask.Failed += FindTask_Failed; FindParameters findParameters = new FindParameters(); findParameters.LayerIds.AddRange(new int[] { 3 }); findParameters.SearchFields.AddRange(new string[] { "NAME" }); findParameters.ReturnGeometry = true; findParameters.SearchText = FindTextBox.Text; findTask.ExecuteAsync(findParameters); }Displaying results
- Declare a handler for the Find task's ExecuteCompleted event. This handler will be invoked when a find operation is complete. A list of FindResults containing information about the features with matching attributes is passed to the handler's args parameter. 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.private void FindTask_ExecuteCompleted(object sender, FindEventArgs args) { }- Get a reference to the results GraphicsLayer and clear any previously added graphics from it.
private void FindTask_ExecuteCompleted(object sender, FindEventArgs args) { GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer; graphicsLayer.ClearGraphics(); }- Check whether any results were found.
private void FindTask_ExecuteCompleted(object sender, FindEventArgs args) { GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer; graphicsLayer.ClearGraphics(); if (args.FindResults.Count > 0) { } else { } }- If results were found, loop through them. Apply the results fill symbol you declared in the page's XAML to each feature. Then add it to the results GraphicsLayer.
private void FindTask_ExecuteCompleted(object sender, FindEventArgs args) { GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer; graphicsLayer.ClearGraphics(); if (args.FindResults.Count > 0) { foreach (FindResult result in args.FindResults) { result.Feature.Symbol = ResultsFillSymbol; graphicsLayer.Graphics.Add(result.Feature); } } else { } }- If no features were found, notify the user with a MessageBox.
private void FindTask_ExecuteCompleted(object sender, FindEventArgs args) { GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer; graphicsLayer.ClearGraphics(); if (args.FindResults.Count > 0) { foreach (FindResult result in args.FindResults) { result.Feature.Symbol = ResultsFillSymbol; graphicsLayer.Graphics.Add(result.Feature); } } else { MessageBox.Show("No features found"); } }Handling execution errors
- Declare a handler for the Find task's Failed event. This handler will be invoked if there is a problem with executing a find operation.
private void FindTask_Failed(object sender, TaskFailedEventArgs args) { }- Notify the user of the problem with a MessageBox
private void FindTask_Failed(object sender, TaskFailedEventArgs args) { MessageBox.Show("Find failed: " + args.Error); }