首页 > 代码库 > query

query

前台

<UserControl x:Class="ArcGISSilverlightSDK.AttributeQuery"     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="http://www.mamicode.com/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" WrapAround="True" Extent="-15000000,2000000,-7000000,8000000">             <esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer"                       Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/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">             <Border.Effect>                 <DropShadowEffect/>             </Border.Effect>             <Grid x:Name="QueryGrid" HorizontalAlignment="Right" VerticalAlignment="Top" >                 <Grid.RowDefinitions>                     <RowDefinition Height="30" />                     <RowDefinition Height="30" />                     <RowDefinition Height="*" />                 </Grid.RowDefinitions>                 <TextBlock x:Name="DataDisplayTitleBottom" Text="Query a layer (search for a state)"                        Foreground="White" FontSize="10" Grid.Row="0"                                             Margin="15,5,15,1" HorizontalAlignment="Center" >                     <TextBlock.Effect>                         <DropShadowEffect />                                              </TextBlock.Effect>                 </TextBlock>                 <ComboBox x:Name="QueryComboBox" Grid.Row="1" MinWidth="150" SelectionChanged="QueryComboBox_SelectionChanged"                          Margin="5,1,5,5" >                 </ComboBox>                 <ScrollViewer x:Name="DataGridScrollViewer" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Auto"                           Width="230" MinHeight="200" Visibility="Collapsed" Grid.Row="2">                     <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>                 </ScrollViewer>             </Grid>         </Border>

    </Grid> </UserControl>

 

后台

using System; using System.Windows; using System.Windows.Controls; using ESRI.ArcGIS.Client; using ESRI.ArcGIS.Client.Tasks;

namespace ArcGISSilverlightSDK {     public partial class AttributeQuery : UserControl     {         public AttributeQuery()         {             InitializeComponent();

            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;

            ESRI.ArcGIS.Client.Tasks.Query query = new ESRI.ArcGIS.Client.Tasks.Query();

            // Specify fields to return from initial query             query.OutFields.AddRange(new string[] { "STATE_NAME" });

            // This query will just populate the drop-down, so no need to return geometry             query.ReturnGeometry = false;

            // Return all features             query.Where = "1=1";             queryTask.ExecuteAsync(query, "initial");         }

        private void QueryComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)         {             if (QueryComboBox.SelectedItem.ToString().Contains("Select..."))                 return;

            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;                    ESRI.ArcGIS.Client.Tasks.Query query = new ESRI.ArcGIS.Client.Tasks.Query();       query.ReturnGeometry = true;             query.Text = QueryComboBox.SelectedItem.ToString();             query.OutSpatialReference = MyMap.SpatialReference;             query.OutFields.Add("*");

            queryTask.ExecuteAsync(query);         }

        private void QueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args)         {             FeatureSet featureSet = args.FeatureSet;

            // If initial query to populate states combo box             if ((args.UserState as string) == "initial")             {                 // Just show on initial load                 QueryComboBox.Items.Add("Select...");

                foreach (Graphic graphic in args.FeatureSet.Features)                 {                     QueryComboBox.Items.Add(graphic.Attributes["STATE_NAME"].ToString());                 }

                QueryComboBox.SelectedIndex = 0;                 return;             }

            // Remove the first entry if "Select..."             if (QueryComboBox.Items[0].ToString().Contains("Select..."))                 QueryComboBox.Items.RemoveAt(0);

            // If an item has been selected                        GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;             graphicsLayer.Graphics.Clear();

            if (featureSet != null && featureSet.Features.Count > 0)             {                 // Show selected feature attributes in DataGrid                 Graphic selectedFeature = featureSet.Features[0];

                QueryDetailsDataGrid.ItemsSource = selectedFeature.Attributes;

                // Highlight 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);

                // If DataGrid not visible (initial load), show it                 if (DataGridScrollViewer.Visibility == Visibility.Collapsed)                 {                     DataGridScrollViewer.Visibility = Visibility.Visible;                     QueryGrid.Height = Double.NaN;                     QueryGrid.UpdateLayout();                 }             }             else             {                 QueryDetailsDataGrid.ItemsSource = null;                 DataGridScrollViewer.Visibility = Visibility.Collapsed;                 QueryGrid.Height = Double.NaN;                 QueryGrid.UpdateLayout();             }         }

        private void QueryTask_Failed(object sender, TaskFailedEventArgs args)         {             MessageBox.Show("Query failed: " + args.Error);         }     } }

query