検索タスクを使用するとマップ内の 1 つ以上のレヤから検索条件に一致するフゖーチャを取得するこ とができます。取得したフゖーチャの図形情報や属性情報は、.NET コードを使用して Silverlight ゕプリ ケーション上に表示します。開発者は検索タスクを使用するためのユーザ ンタフェースと実行ロジック を実装する必要があります。
検索タスク(サンプル コード)
以下は検索タスクを実行する Silverlight ゕプリケーションの XAML と .NET コード(C#)です。このゕ プリケーションでは TextBox コントロールを使用して検索条件を指定し、実行ボタンによってタスクを実 行します。実行結果はマップ チップを有効化したグラフゖックス レヤに追加されます。サンプル コ ードの詳細については「検索タスク(サンプル コード)の解説」をご参照ください。
83 XAML
<UserControl x:Class="Sample.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">
<Grid x:Name="LayoutRoot" Background="White">
<!-- 検索タスクの実行結果用シンボル -->
<Grid.Resources>
<esri:SimpleFillSymbol x:Name="ResultsFillSymbol" Fill="#64FF0000"
BorderBrush="Red" BorderThickness="2" />
</Grid.Resources>
<!-- マップ -->
<esri:Map x:Name="MyMap" Extent="-125, 25, -65, 55" >
<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 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="人口 (2007): " />
<TextBlock Text="{Binding [POP2007]}" />
</StackPanel>
</StackPanel>
<Border BorderBrush="Black" BorderThickness="1" />
</Grid>
84 </esri:GraphicsLayer.MapTip>
</esri:GraphicsLayer>
</esri:Map.Layers>
</esri:Map>
<!-- 検索タスク ンタフェース -->
<Canvas HorizontalAlignment="Right" VerticalAlignment="Top"
Margin="0,15,7,0" Width="230" >
<Rectangle Fill="#CC5C90B2" Stroke="Gray" RadiusX="10" RadiusY="10"
Width="210" Height="55" />
<TextBlock Text="次の文字列を含む郡を検索: " 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="検索" Margin="168,23,0,0"
Click="FindButton_Click" />
</Canvas>
</Grid>
</UserControl>
85 コードビハインド(C#)
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Tasks;
namespace Sample {
public partial class MainPage : UserControl {
public MainPage() {
InitializeComponent();
}
// 検索ボタンがクリックされたらタスクを実行
private void FindButton_Click(object sender, RoutedEventArgs e) {
// 検索タスクの初期化
FindTask findTask = new
FindTask("http://sampleserver1.arcgisonline.com/ArcGIS/
rest/services/" + "Demographics/ESRI_Census_USA/MapServer/");
findTask.ExecuteCompleted += FindTask_ExecuteCompleted;
findTask.Failed += FindTask_Failed;
// 検索パラメータの初期化。検索を行うフゖールドとして Name 列を指定しています。
FindParameters findParameters = new FindParameters();
findParameters.LayerIds.AddRange(new int[] { 3 });
findParameters.SearchFields.AddRange(new string[] { "NAME" });
findParameters.ReturnGeometry = true;
// TextBox のテキストを検索条件として指定
findParameters.SearchText = FindTextBox.Text;
86 findTask.ExecuteAsync(findParameters);
}
// 検索が完了したら結果を描画
private void FindTask_ExecuteCompleted(object sender, FindEventArgs args) {
// 前回の検索結果をクリゕ
GraphicsLayer graphicsLayer =
MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
graphicsLayer.ClearGraphics();
// 新規検索結果をチェック
if (args.FindResults.Count > 0) {
// マップに結果を追加
foreach (FindResult result in args.FindResults) {
result.Feature.Symbol = ResultsFillSymbol;
graphicsLayer.Graphics.Add(result.Feature);
} } else {
MessageBox.Show("該当するレコードが見つかりません");
} }
// 検索が失敗したことを通知
private void FindTask_Failed(object sender, TaskFailedEventArgs args) {
MessageBox.Show("検索が失敗しました: " + args.Error);
} } }
87
検索タスク(サンプル コード)の解説
以下では前述したサンプル コードの内容について解説します。コードの全文についてはこちらをご参照く ださい。
検索タスクの入力インタフェースの作成
タスクは固定のユーザ ンタフェースを持たないため、開発者は自身でユーザが検索条件を入力し検索タ スクを実行するためのンタフェースを作成する必要があります。このサンプル コードでは、検索条件を 入力するための TextBox コントロール、タスクを実行するための Button コントロールを作成します。
1. ゕプリケーションの XAML フゔルにンタフェースを保持する Canvas コントロールを追 加します。
<Canvas HorizontalAlignment="Right" VerticalAlignment="Top"
Margin="0,15,7,0" Width="230" >
</Canvas>
ンタフェースの背景に使用する Rectangle コントロールを追加します。
2.
<Canvas HorizontalAlignment="Right" VerticalAlignment="Top"
Margin="0,15,7,0" Width="230" >
<Rectangle Fill="#CC5C90B2" Stroke="Gray" RadiusX="10" RadiusY="10"
Width="210" Height="55" />
</Canvas>
ユーザに使用方法を通知する TextBlock コントロールを追加します。
3.
<Canvas HorizontalAlignment="Right" VerticalAlignment="Top"
Margin="0,15,7,0" Width="230" >
<Rectangle Fill="#CC5C90B2" Stroke="Gray" RadiusX="10" RadiusY="10"
Width="210" Height="55" />
<TextBlock Text="次の文字列を含む郡を検索: " Foreground="White"
FontSize="10" Margin="10,5,0,0" />
</Canvas>
88 検索条件を入力する TextBox コントロールを追加します。
4.
<Canvas HorizontalAlignment="Right" VerticalAlignment="Top"
Margin="0,15,7,0" Width="230" >
<Rectangle Fill="#CC5C90B2" Stroke="Gray" RadiusX="10" RadiusY="10"
Width="210" Height="55" />
<TextBlock Text="次の文字列を含む郡を検索: " Foreground="White"
FontSize="10" Margin="10,5,0,0" />
<TextBox x:Name="FindTextBox" Width="150" Margin="15,22,0,0" />
</Canvas>
TextBox コントロールの Text プロパテゖに既定の検索条件を指定します。
5.
<Canvas HorizontalAlignment="Right" VerticalAlignment="Top"
Margin="0,15,7,0" Width="230" >
<Rectangle Fill="#CC5C90B2" Stroke="Gray" RadiusX="10" RadiusY="10"
Width="210" Height="55" />
<TextBlock Text="次の文字列を含む郡を検索: " Foreground="White"
FontSize="10" Margin="10,5,0,0" />
<TextBox x:Name="FindTextBox" Width="150" Margin="15,22,0,0" Text="Wash" />
</Canvas>
検索タスクを実行するための Button コントロールを追加します。
6.
<Canvas HorizontalAlignment="Right" VerticalAlignment="Top"
Margin="0,15,7,0" Width="230" >
<Rectangle Fill="#CC5C90B2" Stroke="Gray" RadiusX="10" RadiusY="10"
Width="210" Height="55" />
<TextBlock Text="次の文字列を含む郡を検索: " 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="検索" Margin="168,23,0,0" />
</Canvas>
89 Button コントロールがクリックされた際のベントハンドラをゕタッチします(ハンドラの実 7.
装方法についてはこの後のセクションで解説します)。
<Canvas HorizontalAlignment="Right" VerticalAlignment="Top"
Margin="0,15,7,0" Width="230" >
<Rectangle Fill="#CC5C90B2" Stroke="Gray" RadiusX="10" RadiusY="10"
Width="210" Height="55" />
<TextBlock Text="次の文字列を含む郡を検索: " 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="検索" Margin="168,23,0,0"
Click="FindButton_Click" />
</Canvas>
90 検索タスクの出力インタフェースの作成
タスクの実行結果を表示するためには、出力ンタフェースを作成する必要があります。サンプル ゕプリ ケーションでは、マップにグラフゖックス レヤを追加し、タスクの結果を描画します。また結果のグラ フゖックの属性情報を表示するためにグラフゖックス レヤにマップ チップを追加します。
1. 検索結果に使用するシンボルをリソースとして定義します(シンボルについての詳細は「シン ボルとレンダラ」を参照してください)。
<Grid.Resources>
<esri:SimpleFillSymbol x:Name="ResultsFillSymbol" Fill="#500000FF"
BorderBrush="Blue" BorderThickness="1" />
</Grid.Resources>
2. 以下の手順では、下記の例のように XAML にマップが追加されており、背景用のマップ サー ビス レヤが 1 つ追加されている状態から出力ンタフェースを作成する手順について解説 します。
<esri:Map x:Name="MyMap" Extent="-125, 25, -65, 55" >
<esri:Map.Layers>
<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer"
Url="http://server.arcgisonline.com/ArcGIS/rest/
services/ESRI_StreetMap_World_2D/MapServer"/>
</esri:Map.Layers>
</esri:Map>
マップに結果を表示するためのグラフゖックス レヤとして GraphicsLayer クラスを追加し 3.
ます (ID は 「MyGraphicsLayer」)。GraphicsLayer クラスが 他のレヤよりも上に表示さ れるように他のマップ内のレヤ クラスよりも下の行に追加されています。
<esri:Map x:Name="MyMap" Extent="-125, 25, -65, 55" >
<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>
</esri:Map.Layers>
</esri:Map>
91 GraphicsLayer クラスの MapTip プロパテゖを設定します。MapTip プロパテゖにはマップ 4.
チップの背景と枠線を定義することができます。以下の例では Grid コントロール内に StackPanel コントロールと Border コントロールを配置して、単純な長方形のマップ チップ を作成しています。マップ チップの背景色は Grid コントロールの Background プロパテゖ に指定し、枠線色は Border コントロールの BorderBrush プロパテゖに指定します。マップ チップのコンテナをこのように定義すると、マップ チップの内容に応じてコンテナのサズは 自動的に変更されます。以下のステップではこのコンテナ内に郡名、州名および郡の人口が表 示されるように設定を行います。
<esri:Map x:Name="MyMap" Extent="-125, 25, -65, 55" >
<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 Margin="5">
</StackPanel>
<Border BorderBrush="Black" BorderThickness="1" />
</Grid>
</esri:GraphicsLayer.MapTip>
</esri:GraphicsLayer>
</esri:Map.Layers>
</esri:Map>
92 Orientation プロパテゖを Horizontal (水平)に指定した StackPanel コントロールを追加し 5.
ます。この StackPanel コントロールは検索結果レコードの郡名および州名を格納します。
<esri:Map x:Name="MyMap" Extent="-125, 25, -65, 55" >
<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 Margin="5">
<StackPanel Orientation="Horizontal">
</StackPanel>
</StackPanel>
<Border BorderBrush="Black" BorderThickness="1" />
</Grid>
</esri:GraphicsLayer.MapTip>
</esri:GraphicsLayer>
</esri:Map.Layers>
</esri:Map>
93 郡名を保持するための TextBlock コントロールを追加します。郡名は太字で表示します 6.
(FontWeight="Bold")。
<esri:Map x:Name="MyMap" Extent="-125, 25, -65, 55" >
<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 Margin="5">
<StackPanel Orientation="Horizontal">
<TextBlock FontWeight="Bold" />
</StackPanel>
</StackPanel>
<Border BorderBrush="Black" BorderThickness="1" />
</Grid>
</esri:GraphicsLayer.MapTip>
</esri:GraphicsLayer>
</esri:Map.Layers>
</esri:Map>
94 追加した TextBlock コントロールの Text プロパテゖにグラフゖックの属性をバンドしま 7.
す。グラフゖックの属性は、フゖールド名としての Key と属性値としての Value のペゕのコ レクションです。ここでは NAME 列を Key として指定し、NAME フゖールドの属性値を Text プロパテゖにバンドします。
<esri:Map x:Name="MyMap" Extent="-125, 25, -65, 55" >
<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 Margin="5">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding [NAME]}" FontWeight="Bold" />
</StackPanel>
</StackPanel>
<Border BorderBrush="Black" BorderThickness="1" />
</Grid>
</esri:GraphicsLayer.MapTip>
</esri:GraphicsLayer>
</esri:Map.Layers>
</esri:Map>
95 郡名のあとに続く「County, 」文字列を保持する TextBlock コントロールを追加します。
8.
<esri:Map x:Name="MyMap" Extent="-125, 25, -65, 55" >
<esri:Map.Layers>
<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer"
Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_Wor ld_2D/MapServer"/>
<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" />
</StackPanel>
</StackPanel>
<Border BorderBrush="Black" BorderThickness="1" />
</Grid>
</esri:GraphicsLayer.MapTip>
</esri:GraphicsLayer>
</esri:Map.Layers>
</esri:Map>
96 州名を表示する TextBlock コントロールを追加し、Text プロパテゖに STATE_NAME 列の値 9.
をバンドします。
<esri:Map x:Name="MyMap" Extent="-125, 25, -65, 55" >
<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 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>
<Border BorderBrush="Black" BorderThickness="1" />
</Grid>
</esri:GraphicsLayer.MapTip>
</esri:GraphicsLayer>
</esri:Map.Layers>
</esri:Map>