Select to view content in your preferred language

Find & query

1217
11
07-19-2010 01:35 PM
TonyAlmeida
MVP Regular Contributor
I would like to query a layer, but also incorporate a find. Just like the "Find" and "Query attribute" examples but into one. Anyone have a simple one that's put together that you can share please?
0 Kudos
11 Replies
TonyAlmeida
MVP Regular Contributor
How I can i modify the "Attribute Query" to instead of a drop down and select to just a search, input box. I would like to keep the "Attribute Query" functionality except make it into a search?
0 Kudos
dotMorten_esri
Esri Notable Contributor
Replace the combobox with a textbox, add a "search" button and when clicked use the text in the textbox as the input, instead of the selected item in the combobox
0 Kudos
PaulLeedham
Deactivated User
Instead of using the dropdown list value as your query text use the text from a textbox then use a button click event to begin your search. Here is an example, using the syntax from the Attribute Query sample:

Dim query As New ESRI.ArcGIS.Client.Tasks.Query()
query.OutFields.Add("*")
query.Text = QueryComboBox.SelectedItem.ToString() (Change this to: TextBox.Text)

queryTask.ExecuteAsync(query)

You may also want to apply auto suggest to your text box to help users refine their searches.

Thanks,

Paul Leedham
City of Hudson
http://gis.hudson.oh.us/
0 Kudos
TonyAlmeida
MVP Regular Contributor
pleedham thanks for the replay, i am still unable to correctly make this work... Here is my current xaml code.
<UserControl x:Class="SilverlightApplication1.MainPage"
    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/client/2009"
    xmlns:slData="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data">
    <Grid x:Name="LayoutRoot" Background="White">

        <Grid.Resources>
            <esri:SimpleFillSymbol x:Key="DefaultFillSymbol" Fill="#500000FF" BorderBrush="Blue" BorderThickness="1" />
        </Grid.Resources>

        <esri:Map x:Name="MyMap" Extent="-130,10,-70,60" >
            <esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer"
                      Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
            <esri:GraphicsLayer ID="MyGraphicsLayer" />
        </esri:Map>

        <Border x:Name="QueryBorder" Background="#77919191" BorderThickness="1" CornerRadius="5"
                HorizontalAlignment="Right" BorderBrush="Gray" VerticalAlignment="Top"
                Margin="5">
       
            <Grid x:Name="QueryGrid" HorizontalAlignment="Right" VerticalAlignment="Top" >
                <Grid.RowDefinitions>
                    <RowDefinition Height="30" />
                    <RowDefinition Height="30" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                        <TextBlock Text="US State Name contains:" Margin="10,0,0,0" VerticalAlignment="Center"/>
                        <TextBox x:Name="TextBox" Text="New" Height="23" HorizontalAlignment="Left" VerticalAlignment="Center" Width="125" TextWrapping="NoWrap"
                     Margin="10,0,10,0" FontSize="12" Background="White" AcceptsReturn="False" />
                        <Button Content="Do Query" Width="75" VerticalAlignment="Center" HorizontalAlignment="Right" Click="QueryButton_Click" Margin="0,0,10,0" Cursor="Hand" />
             

                    <slData:DataGrid x:Name="QueryDetailsDataGrid" AutoGenerateColumns="False" HeadersVisibility="None"
                              Background="White">
                        <slData:DataGrid.Columns>
                            <slData:DataGridTextColumn Binding="{Binding Path=Key}" FontWeight="Bold"/>
                            <slData:DataGridTextColumn Binding="{Binding Path=Value}"/>
                        </slData:DataGrid.Columns>
                    </slData:DataGrid>            
            </Grid>
        </Border>

    </Grid>
</UserControl>
0 Kudos
PaulLeedham
Deactivated User
Can you provide a copy of your "QueryButton_Click" code from the code-behind page? This is where you create your query task so I assume this is where your problems a coming from.
Thanks,

Paul Leedham
0 Kudos
TonyAlmeida
MVP Regular Contributor
I tried a different example and it seems to work ok except for the DataGridColumn displaying over the text box. Also when i try to search by State i get the following error "Query Failed.ESRI.ArcGIS.Client.Tasks.ServiceException??? Here is my current xaml.

<UserControl x:Class="SilverlightApplicationFindQuery.MainPage"
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"
             xmlns:slData="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data">
   

    <Grid x:Name="LayoutRoot" Background="White">

        <!-- QUERY TASK OUTPUT RESOURCES -->

        <Grid.Resources>
            <esriConverters:DictionaryConverter x:Name="MyDictionaryConverter" />
            <esriSymbols:SimpleFillSymbol x:Name="ResultsFillSymbol" Fill="#500000FF" BorderBrush="Blue"
  BorderThickness="1" />

        </Grid.Resources>

        <!-- MAP -->
        <esri:Map x:Name="MyMap" Extent="-80.05,36.93,-68.79,43.18" >
            <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>
                                <TextBlock Text="{Binding Converter={StaticResource MyDictionaryConverter},
       ConverterParameter=STATE_NAME, Mode=OneWay}" FontWeight="Bold" />
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="Population Density (2007): " />
                                    <TextBlock Text="{Binding Converter={StaticResource MyDictionaryConverter},
        ConverterParameter=POP07_SQMI, Mode=OneWay}" />
                                </StackPanel>
                            </StackPanel>                          
                            <Border BorderBrush="Black" BorderThickness="1" />                          
                        </Grid>
                    </esri:GraphicsLayer.MapTip>
                </esri:GraphicsLayer>
            </esri:Map.Layers>
        </esri:Map>
       
       
       
<!-- QUERY TASK INTERFACE -->
        <Canvas HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,15,7,0" Width="250" >
            <Rectangle Fill="#CC5C90B2" Stroke="Gray"  RadiusX="10" RadiusY="10" Width="230" Height="55" />
            <TextBlock Text="Type a query and click Execute" Foreground="White" FontSize="10" Margin="10,5,0,0" />
            <TextBox x:Name="QueryTextBox" Width="150" Margin="15,22,0,0" Text="POP07_SQMI > 500" />
            <Button x:Name="QueryButton" Content="Execute" Margin="168,23,0,0" Click="QueryButton_Click" />
            <slData:DataGrid x:Name="QueryDetailsDataGrid" AutoGenerateColumns="False" HeadersVisibility="None"
                              Background="White">
                <slData:DataGrid.Columns>
                    <slData:DataGridTextColumn Binding="{Binding Path=Key}" FontWeight="Bold"/>
                    <slData:DataGridTextColumn Binding="{Binding Path=Value}"/>
                </slData:DataGrid.Columns>
            </slData:DataGrid>
        </Canvas>
    </Grid>
</UserControl>
0 Kudos
TonyAlmeida
MVP Regular Contributor
Code behind page.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Tasks;
using ESRI.ArcGIS.Client.Symbols;
using ESRI.ArcGIS.Client.ValueConverters;



namespace SilverlightApplicationFindQuery
{
    public partial class MainPage : UserControl
    {
        public MainPage()  {InitializeComponent();}

            private void QueryButton_Click(object sender, RoutedEventArgs e)
               {
            QueryTask queryTask = new QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +
      "Demographics/ESRI_Census_USA/MapServer/5");
                queryTask.ExecuteCompleted += QueryTask_ExecuteCompleted;
                queryTask.Failed += QueryTask_Failed;

                Query query = new Query();
                query.ReturnGeometry = true;
                query.OutFields.AddRange(new string[] { "STATE_NAME", "POP07_SQMI" });
                query.Where = QueryTextBox.Text;

                queryTask.ExecuteAsync(query);
            }

            // Do query when execute button is clicked
            private void QueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args)
            {
                FeatureSet featureSet = args.FeatureSet;
                GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
                graphicsLayer.ClearGraphics();

                if (args.FeatureSet.Features.Count > 0)
                {
                    // Show selected feature attributes in DataGrid
                    Graphic selectedFeature = featureSet.Features[0];
                    QueryDetailsDataGrid.ItemsSource = selectedFeature.Attributes;

                    // Hightlight selected feature
                selectedFeature.Symbol = LayoutRoot.Resources["DefaultFillSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
                graphicsLayer.Graphics.Add(selectedFeature);

                // Zoom to selected feature (define expand percentage)
                ESRI.ArcGIS.Client.Geometry.Envelope selectedFeatureExtent = selectedFeature.Geometry.Extent;

                double expandPercentage = 30;

                double widthExpand = selectedFeatureExtent.Width * (expandPercentage / 100);
                double heightExpand = selectedFeatureExtent.Height * (expandPercentage / 100);

                ESRI.ArcGIS.Client.Geometry.Envelope displayExtent = new ESRI.ArcGIS.Client.Geometry.Envelope(
                selectedFeatureExtent.XMin - (widthExpand / 2),
                selectedFeatureExtent.YMin - (heightExpand / 2),
                selectedFeatureExtent.XMax + (widthExpand / 2),
                selectedFeatureExtent.YMax + (heightExpand / 2));

                MyMap.ZoomTo(displayExtent);
                    foreach (Graphic resultFeature in args.FeatureSet.Features)
                    {
                        resultFeature.Symbol = ResultsFillSymbol;
                        graphicsLayer.Graphics.Add(resultFeature);
                    }
                }
                else
                    QueryDetailsDataGrid.ItemsSource = null;
                {
                   
                  
                }
            }
                private void QueryTask_Failed(object sender, TaskFailedEventArgs args)
{
                    MessageBox.Show("Query failed: " + args.Error);
       }
   } 
}
0 Kudos
PaulLeedham
Deactivated User
In order for your state name query to work you will need to have the query syntax something like:
query.Where = "STATE_NAME LIKE '%" +QueryTextBox.Text+ "%'"

As you can see from the example above the where statement needs to contain the field name and texbox syntax.  I apologize if I did not make that clear in my last posting.  I would recommend using just one field to search against unless you want to do some string interogation prior to creating the where string--you could check if the user is searching for numbers or strings and change the search field appropriately.

Your grid is being placed on-top of your search box because you have not configured your grid display margins correctly.  You may want to place everything in a vertical stackpanel so that the grid is displayed below the textbox.  In order to do this, you may want to add some logic to the display of the grid.  For Example, only showing the grid if there are some features selected--try using a border around your grid and toggling the control to visible or not visible.  There are other samples that use this method on the ESRI samples page.

Thanks,

Paul Leedham
http://gis.hudson.oh.us/
0 Kudos
TonyAlmeida
MVP Regular Contributor
Sweet! I got the grid displaying underneath the search box. How can i get it to show all the attributes for that layer and make it scroll down and up?
0 Kudos