Find

Download Samples Repository

Description

This sample demonstrates how to search your data using the find task. The sample displays the results on the map and in tabular list view.

"Desktop" "Store" "Phone" Available for Desktop, Store, Phone

Sample Code

<UserControl x:Class="ArcGISRuntime.Samples.Desktop.FindTaskSample"
             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">
    <Grid x:Name="layoutGrid">
        <Grid.Resources>
            <esri:SimpleMarkerSymbol x:Key="MarkerSymbol" Size="6" Color="Red" Style="Circle" />
            <esri:SimpleLineSymbol x:Key="LineSymbol" Color="Red" Width="4"  />
            <esri:SimpleFillSymbol x:Key="FillSymbol" Color="#50FF0000">
                <esri:SimpleFillSymbol.Outline>
                    <esri:SimpleLineSymbol Color="Red" Width="2" Style="Solid" />
                </esri:SimpleFillSymbol.Outline>
            </esri:SimpleFillSymbol>
        </Grid.Resources>

        <esri:MapView x:Name="MyMapView" WrapAround="True">
            <esri:Map InitialViewpoint="-14675766.357,2695407.734,-6733121.861,6583994.101,102100">
                <esri:ArcGISTiledMapServiceLayer 
                    ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer" />
				<esri:ArcGISDynamicMapServiceLayer 
                     ServiceUri="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer" />
			</esri:Map>
			<esri:MapView.GraphicsOverlays>
				<esri:GraphicsOverlay ID="graphicsOverlay" />
			</esri:MapView.GraphicsOverlays>
		</esri:MapView>

        <Border Background="White" BorderBrush="Black" BorderThickness="1" 
				Margin="30" Padding="20" Width="325"
                HorizontalAlignment="Right" VerticalAlignment="Top">
			<Border.Effect>
				<DropShadowEffect />
			</Border.Effect>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>

                <StackPanel Grid.Row="0" Orientation="Horizontal" HorizontalAlignment="Center">
                    <TextBlock Text="Find State/City/River:" Margin="0,0,6,0" VerticalAlignment="Center" />
                    <TextBox x:Name="txtFind" Text="Colorado" Width="100" />
                </StackPanel>
                
                <Button Grid.Row="1" Content="Find" IsDefault="True" Margin="24,8" Click="FindButton_Click" />

                <ProgressBar x:Name="progress" Grid.Row="2" IsIndeterminate="True" Margin="10" Visibility="Collapsed"/>

                <ListView x:Name="resultsGrid" Grid.Row="2" Margin="0,20,0,0" Visibility="Collapsed" ItemsSource="{x:Null}" 
                          SelectionMode="Single" SelectionChanged="resultsGrid_SelectionChanged">
                    <ListView.View>
                        <GridView>
                            <GridViewColumn Header="Layer" DisplayMemberBinding="{Binding LayerName}" Width="50" />
                            <GridViewColumn Header="Field" DisplayMemberBinding="{Binding FoundFieldName}" Width="90" />
                            <GridViewColumn Header="Value" DisplayMemberBinding="{Binding Value}" Width="200" />
                        </GridView>
                    </ListView.View>
                </ListView>
            </Grid>
        </Border>
    </Grid>
</UserControl>
using Esri.ArcGISRuntime.Controls;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Layers;
using Esri.ArcGISRuntime.Symbology;
using Esri.ArcGISRuntime.Tasks.Query;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace ArcGISRuntime.Samples.Desktop
{
    /// <summary>
    /// This sample demonstrates how to search your data using the find task. The sample displays the results on the map and in tabular list view.
    /// </summary>
    /// <title>Find</title>
	/// <category>Tasks</category>
	/// <subcategory>Query</subcategory>
	public partial class FindTaskSample : UserControl
    {
        private Symbol _markerSymbol;
        private Symbol _lineSymbol;
        private Symbol _fillSymbol;
		private GraphicsOverlay _graphicsOverlay;

        /// <summary>Construct Find sample control</summary>
        public FindTaskSample()
        {
            InitializeComponent();
            _markerSymbol = layoutGrid.Resources["MarkerSymbol"] as Symbol;
            _lineSymbol = layoutGrid.Resources["LineSymbol"] as Symbol;
            _fillSymbol = layoutGrid.Resources["FillSymbol"] as Symbol;

			_graphicsOverlay = MyMapView.GraphicsOverlays["graphicsOverlay"];
        }

        // Find map service items with entered information in given fields
        private async void FindButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                progress.Visibility = Visibility.Visible;
                resultsGrid.Visibility = Visibility.Collapsed;
                resultsGrid.ItemsSource = null;
				_graphicsOverlay.Graphics.Clear();

                FindTask findTask = new FindTask(
                    new Uri("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer"));

                var param = new FindParameters()
                {
                    LayerIDs = new List<int> { 0, 1, 2 },
                    SearchFields = new List<string> { "CITY_NAME", "NAME", "SYSTEM", "STATE_ABBR", "STATE_NAME" },
                    ReturnGeometry = true,
                    SpatialReference = MyMapView.SpatialReference,
                    SearchText = txtFind.Text
                };

                var findResults = await findTask.ExecuteAsync(param);
                if (findResults != null && findResults.Results.Count > 0)
                {
                    resultsGrid.ItemsSource = findResults.Results;
                    resultsGrid.Visibility = Visibility.Visible;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Find Sample");
            }
            finally
            {
                progress.Visibility = Visibility.Collapsed;
            }
        }

        // Highlight the selected grid view item on the map
        private void resultsGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
			_graphicsOverlay.Graphics.Clear();

            if (e.AddedItems != null && e.AddedItems.Count > 0)
            {
                var findItem = e.AddedItems.OfType<FindItem>().FirstOrDefault();
                if (findItem != null)
					_graphicsOverlay.Graphics.Add(new Graphic(findItem.Feature.Geometry, ChooseGraphicSymbol(findItem.Feature.Geometry)));
            }
        }

        // Select a marker / line / fill symbol based on geometry type
        private Symbol ChooseGraphicSymbol(Geometry geometry)
        {
            if (geometry == null)
                return null;

            Symbol symbol = null;
            switch (geometry.GeometryType)
            {
                case GeometryType.Point:
                case GeometryType.Multipoint:
                    symbol = _markerSymbol;
                    break;

                case GeometryType.Polyline:
                    symbol = _lineSymbol;
                    break;

                case GeometryType.Polygon:
                case GeometryType.Envelope:
                    symbol = _fillSymbol;
                    break;
            }

            return symbol;
        }
    }
}
Feedback on this topic?