属性查询

Mainpage
<UserControl x:Class="AttributeQuery.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    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" WrapAround="True" Extent="-128,32,-114,43" >
            <esri:ArcGISDynamicMapServiceLayer ID="layer" 
                Url="http://localhost:6080/arcgis/rest/services/California/MapServer"/>
            <esri:GraphicsLayer ID="MyGraphicsLayer" />
        </esri:Map>
        <Border x:Name="arrtibute" 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="选择图层信息" 
                       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.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;

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

            QueryTask queryTask = new QueryTask("http://localhost:6080/arcgis/rest/services/California/MapServer/0");
            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[] { "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://localhost:6080/arcgis/rest/services/California/MapServer/0");
            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["NAME"].ToString());
                }

                QueryComboBox.SelectedIndex = 0;
                return;
            }

            if (QueryComboBox.Items[0].ToString().Contains("选择..."))
                QueryComboBox.Items.RemoveAt(0);

            GraphicsLayer graphicsLayer = Mymap.Layers["MyGraphicsLayer"] as GraphicsLayer;
            graphicsLayer.ClearGraphics();


            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 (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);
        }
    }
}


实例二

<UserControl x:Class="MapFind.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    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:SimpleMarkerSymbol x:Key="DefaultMarkerSymbol" Size="8" Color="Red" Style="Circle" />
            <esri:SimpleLineSymbol x:Key="DefaultLineSymbol" Color="Red" Width="6"  />
            <esri:SimpleFillSymbol x:Key="DefaultFillSymbol" BorderBrush="Red" BorderThickness="2" Fill="#50FF0000"/>
        </Grid.Resources>

        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="150" />
        </Grid.RowDefinitions>

        <esri:Map x:Name="MyMap" Grid.Row="0" WrapAround="True" Extent="-128,32,-114,43">
            <esri:ArcGISDynamicMapServiceLayer ID="DemographicLayer" Opacity="0.5"
                    Url="http://localhost:6080/arcgis/rest/services/California/MapServer" />
            <esri:GraphicsLayer ID="MyGraphicsLayer" />
        </esri:Map>


        <Border BorderBrush="Black" Grid.Row="0" CornerRadius="5" BorderThickness="1" Height="35" MinWidth="550" HorizontalAlignment="Center" VerticalAlignment="Top"
                Background="#77919191" Margin="10">
            <Border.Effect>
                <DropShadowEffect ShadowDepth="1" />
            </Border.Effect>
            <Grid MinWidth="550">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <TextBlock Text="查询" Foreground="White" Grid.Column="0"
                           HorizontalAlignment="Center" Height="24" VerticalAlignment="Center" 
                           FontWeight="Bold" FontSize="12" Margin="20,8,5,0"/>
                <TextBox x:Name="FindText" Background="White" Text="" Height="23" Width="100" HorizontalContentAlignment="Center" Grid.Column="1" />
                <TextBlock Text="在图层飞机场、城市、地震历史记录查询" Foreground="White"  Grid.Column="2"
                           HorizontalAlignment="Center" Height="24" VerticalAlignment="Center" 
                           FontWeight="Bold" FontSize="12" Margin="5,8,5,0"/>
                <Button x:Name="ExecuteButton" Content="查询" Width="75" Height="24" VerticalAlignment="Center" Click="ExecuteButton_Click"  
                        Margin="5,0,5,0" Cursor="Hand"  Grid.Column="3" />
            </Grid>
        </Border>
        <slData:DataGrid x:Name="FindDetailsDataGrid" AutoGenerateColumns="False" HeadersVisibility="All" Background="White" 
                         BorderBrush="Black" BorderThickness="1" SelectionChanged="FindDetails_SelectionChanged" 
                         HorizontalScrollBarVisibility="Hidden" Grid.Row="1"
                         IsReadOnly="True" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
                         Height="Auto" Width="Auto">
            <slData:DataGrid.Columns>
                <slData:DataGridTextColumn Binding="{Binding Path=LayerId}" Header="图层号" />
                <slData:DataGridTextColumn Binding="{Binding Path=LayerName}" Header="图层名称"/>
                <slData:DataGridTextColumn Binding="{Binding Path=FoundFieldName}" Header="字段名" />
                <slData:DataGridTextColumn Binding="{Binding Path=Value}" Header="查询值"/>
            </slData:DataGrid.Columns>
        </slData:DataGrid>        
    </Grid>
</UserControl>

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 System.Windows.Data;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Tasks;

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

        private void ExecuteButton_Click(object sender, RoutedEventArgs e)
        {
            GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
            graphicsLayer.ClearGraphics();

            FindTask findTask = new FindTask("http://localhost:6080/arcgis/rest/services/California/MapServer");
            findTask.Failed += FindTask_Failed;

            FindParameters findParameters = new FindParameters();
            // Layer ids to search
            findParameters.LayerIds.AddRange(new int[] { 0, 1, 2 });
            // Fields in layers to search
            findParameters.SearchFields.AddRange(new string[] { "NAME", "STATE", "AREANAME", "AREALAND", "DAMAGE" });
            // Return features in map's spatial reference
            findParameters.SpatialReference = MyMap.SpatialReference;

            // Bind data grid to find results.  Bind to the LastResult property which returns a list
            // of FindResult instances.  When LastResult is updated, the ItemsSource property on the 
            // will update.  
            Binding resultFeaturesBinding = new Binding("LastResult");
            resultFeaturesBinding.Source = findTask;
            FindDetailsDataGrid.SetBinding(DataGrid.ItemsSourceProperty, resultFeaturesBinding);

            findParameters.SearchText = FindText.Text;
            findTask.ExecuteAsync(findParameters);

        }

        private void FindTask_Failed(object sender, TaskFailedEventArgs args)
        {
            MessageBox.Show("查找失败: " + args.Error);
        }

        private void FindDetails_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DataGrid dataGrid = sender as DataGrid;

            int selectedIndex = dataGrid.SelectedIndex;
            if (selectedIndex > -1)
            {
                FindResult findResult = (FindResult)FindDetailsDataGrid.SelectedItem;
                Graphic graphic = findResult.Feature;

                switch (graphic.Attributes["Shape"].ToString())
                {
                    case "Polygon":
                        graphic.Symbol = LayoutRoot.Resources["DefaultFillSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
                        break;
                    case "Polyline":
                        graphic.Symbol = LayoutRoot.Resources["DefaultLineSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
                        break;
                    case "Point":
                        graphic.Symbol = LayoutRoot.Resources["DefaultMarkerSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
                        break;
                }

                GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
                graphicsLayer.ClearGraphics();
                graphicsLayer.Graphics.Add(graphic);
            }

        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

地知通

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值