zoukankan      html  css  js  c++  java
  • 【转】ArcGIS API for Silverlight/WPF 2.1学习笔记(五)

     

    2Find示例代码

    1xaml文件:

    //添加Symbol命名空间

    xmlns:esriSymbols="clr-namespace:ESRI.ArcGIS.Client.Symbols;assembly=ESRI.ArcGIS.Client

    //创建Symbol

    <Grid.Resources>

    <esriSymbols:SimpleFillSymbol x:Name="ResultsFillSymbol" Fill="#64FF0000" BorderBrush="Red" BorderThickness="2" />

    </Grid.Resources>

    //创建GraphicsLayer,显示查询结果

    <esri:GraphicsLayer ID="MyGraphicsLayer">

    <esri:GraphicsLayer.MapTip>

    <Grid Background="LightYellow">

                  <StackPanel Margin="5">

                         <StackPanel Orientation="Horizontal">

                                <TextBlock Text="{Binding [NAME]}" FontWeight="Bold" />

                                       <TextBlock Text=" County, " FontWeight="Bold" />

                                       <TextBlock Text="{Binding [STATE_NAME]}" FontWeight="Bold" />

                         </StackPanel>

                         <StackPanel Orientation="Horizontal">

                                <TextBlock Text="Population (2007): " />

                                       <TextBlock Text="{Binding [POP2007]}" />

                                </StackPanel>

                         </StackPanel>

                         <Border BorderBrush="Black" BorderThickness="1" />

                  </Grid>

    </esri:GraphicsLayer.MapTip>

    </esri:GraphicsLayer>

    //Find对话框

    <Canvas HorizontalAlignment="" VerticalAlignment="" Margin="0,15,7,0" Width="230" >

    <Rectangle Fill="#CC5C90B2" Stroke="Gray"  RadiusX="10" RadiusY="10" Width="210" Height="55" />

    <TextBlock Text="Find的国家名:" 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>

     

    2cs文件

    //添加命名空间

    using ESRI.ArcGIS.Client;

    using ESRI.ArcGIS.Client.Tasks;

    // 点击find按钮,执行Find

    private void FindButton_Click(object sender, RoutedEventArgs e)

    {

    //初始化Find task

    FindTask findTask = new FindTask("http://./ArcGIS/rest/services/./MapServer/");

           findTask.ExecuteCompleted += FindTask_ExecuteCompleted;

           findTask.Failed += FindTask_Failed;

           //初始化Find参数:将countries图层的Name字段作为Find Field

           FindParameters findParameters = new FindParameters();

           findParameters.LayerIds.AddRange(new int[] { 3 });

           findParameters.SearchFields.AddRange(new string[] { "NAME" });

           //返回find结果的feature geometry

           findParameters.ReturnGeometry = true;

           //textboxtext作为查询关键字

           findParameters.SearchText = FindTextBox.Text;

           //执行查询

           findTask.ExecuteAsync(findParameters);

    }

     

    //find结束后,将结果绘制在地图上

    private void FindTask_ExecuteCompleted(object sender, FindEventArgs args)

    {

    //获取graphicsLayer,清除先前的查询结果

    GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;

           graphicsLayer.ClearGraphics();

           // Check for new results

           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");

           }

    }

     

    //find失败时,显示失败原因

    private void FindTask_Failed(object sender, TaskFailedEventArgs args)

    {

           MessageBox.Show("Find failed: " + args.Error);

    }

     

    3Identify示例代码

    1xaml文件:

    //添加命名空间

    xmlns:esriTasks="clr-namespace:ESRI.ArcGIS.Client.Tasks;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"

    //定义task resources

    <Grid.Resources>

    <esriSymbols:PictureMarkerSymbol x:Name="IdentifyLocationSymbol" OffsetX="35" OffsetY="35" Source="/Assets/images/i_about.png" />

    <esriSymbols:SimpleFillSymbol x:Name="SelectedFeatureSymbol" Fill="#64FF0000" BorderBrush="Red" BorderThickness="2" />

    </Grid.Resources>

    //创建GraphicsLayer,容纳Identify的地图结果显示

    <esri:GraphicsLayer ID="ResultsGraphicsLayer" />

    <esri:GraphicsLayer ID="IdentifyIconGraphicsLayer" />

    //Identify的属性结果显示界面

    <StackPanel Margin="10" HorizontalAlignment="Left">

    <Grid>

    <Rectangle Fill="#CC5C90B2" Stroke="Gray"  RadiusX="10" RadiusY="10" />

    <TextBlock Text="点击地图identify feature" Foreground="White" FontSize="10"                     Margin="10,5,10,5" />

    <StackPanel x:Name="IdentifyResultsStackPanel" Margin="15,30,15,10" Visibility="Collapsed">

    <TextBlock Text="Select a result from the list to display it" Foreground="White"                         FontSize="10" Margin="0,0,0,5" />

    <ComboBox x:Name="IdentifyComboBox" SelectionChanged="IdentifyComboBox_SelectionChanged" />

    <ScrollViewer MaxHeight="340" Margin="0,10,0,0">

    <slData:DataGrid x:Name="IdentifyDetailsDataGrid" AutoGenerateColumns="False" HeadersVisibility="None" >

    <slData:DataGrid.Columns>

    <slData:DataGridTextColumn Binding="{Binding Path=Key}" FontWeight="Bold"/>

    <slData:DataGridTextColumn Binding="{Binding Path=Value}"/>

    </slData:DataGrid.Columns>

                     </slData:DataGrid>

    </ScrollViewer>

    </StackPanel>

    </Grid>

    </StackPanel>

     

    2cs文件

    //添加命名空间

    using ESRI.ArcGIS.Client.Tasks;

    using ESRI.ArcGIS.Client.Symbols;

    // IdentifyResult

    private List<IdentifyResult> _lastIdentifyResult;

    public MainPage() { InitializeComponent(); }

    // 点击地图,执行Identify

    private void MyMap_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs args)

    {

    //identify的地方(鼠标点击附近)显示一个icon

    GraphicsLayer graphicsLayer = MyMap.Layers["IdentifyIconGraphicsLayer"] as GraphicsLayer;

           graphicsLayer.ClearGraphics();

           ESRI.ArcGIS.Client.Graphic graphic = new ESRI.ArcGIS.Client.Graphic()

           {

                  Geometry = args.MapPoint,

                  Symbol = IdentifyLocationSymbol

           };

           graphicsLayer.Graphics.Add(graphic);

           // 初始化Identify task

           IdentifyTask identifyTask = new IdentifyTask("http://./ArcGIS/rest/services/./MapServer");

           identifyTask.ExecuteCompleted += IdentifyTask_ExecuteCompleted;

           identifyTask.Failed += IdentifyTask_Failed;

           //初始化Identify parameters,指定searchinglayers

           IdentifyParameters identifyParameters = new IdentifyParameters();

           identifyParameters.LayerOption = LayerOption.all;

           // 设置identify parameters:通过地图的现有属性

           identifyParameters.MapExtent = MyMap.Extent;

           identifyParameters.Width = (int)MyMap.ActualWidth;

           identifyParameters.Height = (int)MyMap.ActualHeight;

           //通过点击执行Identify features

           identifyParameters.Geometry = args.MapPoint;

           identifyTask.ExecuteAsync(identifyParameters);

    }

     

    //identify完成后,查询结果的Attribute显示窗口

    private void IdentifyTask_ExecuteCompleted(object sender, IdentifyEventArgs args)

    {

    //清除旧的结果

    IdentifyComboBox.Items.Clear();

           // Check for new results

           if (args.IdentifyResults.Count > 0)

           {

                  IdentifyResultsStackPanel.Visibility = Visibility.Visible;

                  //将结果添加到ComboBox

                  foreach (IdentifyResult result in args.IdentifyResults)

                  {

                         string title = string.Format("{0} ({1})", result.Value.ToString(), result.LayerName);

                         IdentifyComboBox.Items.Add(title);

                  }

                  // Workaround for ComboBox bug

                  IdentifyComboBox.UpdateLayout();

                  // Store the list of identify results

                  _lastIdentifyResult = args.IdentifyResults;

                  // Initialize ComboBox and fire SelectionChanged

                  IdentifyComboBox.SelectedIndex = 0;

    }

           else

           {

                  // Hide ComboBox and attributes DataGrid and notify user

                  IdentifyResultsStackPanel.Visibility = Visibility.Collapsed;

                  MessageBox.Show("No features found");

           }

    }

    【原创】ArcGIS API for Silverlight/WPF 2.1学习笔记(五) - 清风明月 - 老钟的GIS小站
     

    //多个结果时,点击下拉框更改其它结果

    void IdentifyComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)

    {

    //地图上清除先前的结果

    GraphicsLayer graphicsLayer = MyMap.Layers["ResultsGraphicsLayer"] as GraphicsLayer;

    graphicsLayer.ClearGraphics();

           // Check that ComboBox has a selected item.  Needed because SelectionChanged fires

           // when ComboBox.Clear is called.

           if (IdentifyComboBox.SelectedIndex > -1)

           {

                  // Update DataGrid with selected feature's attributes

                  Graphic selectedFeature = _lastIdentifyResult[IdentifyComboBox.SelectedIndex].Feature;

    IdentifyDetailsDataGrid.ItemsSource = selectedFeature.Attributes;

                  // Apply symbol and add selected feature to map

                  selectedFeature.Symbol = SelectedFeatureSymbol;

                  graphicsLayer.Graphics.Add(selectedFeature);

           }

    }

     

    //identify失败时,显示失败原因

    private void IdentifyTask_Failed(object sender, TaskFailedEventArgs args)

    {

           MessageBox.Show("Identify failed: " + args.Error);

    }

     

    4Adress locator示例代码(暂未学)

    5Geometry示例代码(暂未学)

    6Geoprocessing示例代码(暂未学)

    7Route示例代码(暂未学)

  • 相关阅读:
    要如何用[ZT]sendmessage來切換PageControl1 的TabSheet2呢
    Delphi 常用API 函数
    [ZT]如何得到其他程序的Richedit中的RTF数据
    转帖一篇关于DELPHI调试的文章AQTime
    讀取股票資料檔與指標計算方法之封裝
    GC的三代回收机制
    SQL语句的解析过程
    .Net 垃圾回收机制整理
    美国人吃了交通罚单怎么办?
    Ihttphandler,Ihttpmodule
  • 原文地址:https://www.cnblogs.com/changbaishan/p/3305971.html
Copyright © 2011-2022 走看看