Série "Desenvolvimento de aplicativo de mapa para desktop"Parte5 desta série.
Desta vez,na parte 4 adicionaremos uma função para exibir os atributos individuais dos dados referenciados no mapa no aplicativo de mapa criado. O código de exemplo da função apresentada neste artigo está disponível no Esri Japão GitHub para consulta.
Exibição de atributos individuais
Implementaremos uma função para exibir as informações de atributos dofeature selecionado no mapa, permitindo verificar quais valores o feature possui. Desta vez, vamos observar as informações de atributos dos polígonos das zonas escolares primárias e secundárias ou dos pontos das escolas primárias e secundárias que você tocar no mapa.
1. Abra o MainWindows.xaml e implemente o evento GeoViewTapped (adicione GeoViewTapped="MainMapView_GeoViewTapped").
MainWindows.xaml
<esri:MapView x:Name="MainMapView" Map="{Binding Map, Source={StaticResource MapViewModel}}" GeoViewTapped="MainMapView_GeoViewTapped"/>
2. Abra o MainWindows.xaml.cs e adicione as seguintes referências de namespace.
MainWindows.xaml.cs
using Esri.ArcGISRuntime.Data;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.UI.Controls;
using Esri.ArcGISRuntime.UI;
3. Crie o processamento a ser executado no evento GeoViewTapped. Para obter o feature do local tocado, use o método
Tips: IdentifyLayerAsync メソッドに渡す第2引数には、識別する範囲を渡します。タップ地点を中心として、設定したスクリーン座標値の高さおよび幅を持つ正方形が識別範囲です。この値が大きいとタップ地点からより広い範囲を識別し、値が小さいと識別される範囲は狭くなります。
4. MapViewModel.cs に、Callout に表示するコンテンツ(CalloutDefinition)を定義する処理と取得されたフィーチャをハイライト表示する処理を作成します。IdentifyLayersAsync() を実行すると、該当するフィーチャの属性とジオメトリが含まれる GeoElement が返ります。GeoElement に含まれるフィーチャの属性情報を整形して CalloutDefinition を作成します。
MapViewModel.cs
public class MapViewModel : INotifyPropertyChanged
{
private Feature? _selectedFeature;
private FeatureLayer? _selectedLayer;
///中略///
public CalloutDefinition Identify(IReadOnlyList<IdentifyLayerResult> identifyLayerResults)
{
CalloutDefinition? calloutDefinition = null;
try
{
// 既に選択されている(ハイライト表示されている)フィーチャの選択を解除する
if (_selectedFeature != null) {
_selectedLayer!.UnselectFeature(_selectedFeature);
}
if (identifyLayerResults.Count == 0 || identifyLayerResults[0].GeoElements.Count == 0) {
throw new Exception("フィーチャが見つかりません");
}
// IdentifyLayersAsync() で取得したフィーチャを選択する(ハイライト表示する)
_selectedFeature = (Feature)identifyLayerResults[0].GeoElements[0];
_selectedLayer = (FeatureLayer)identifyLayerResults[0].LayerContent;
_selectedLayer.SelectFeature(_selectedFeature);
// レイヤーの属性情報のフィールド名とエイリアスを取得する
// Key にフィールド名、Value にフィールドのエイリアスを格納したディクショナリを作成する
var fields = new Dictionary<string, string>();
for (int i = 0; i < _selectedLayer.FeatureTable!.Fields.Count; i++) {
Field? field = _selectedLayer.FeatureTable.Fields[i];
fields.Add(field.Name, field.Alias);
}
// コールアウトのコンテンツ(タイトル)を設定する
string layerName = _selectedLayer.Name;
// 該当フィーチャの全ての属性情報を結合した文字列を作成する
var attributes = new System.Text.StringBuilder();
if (_selectedFeature.Attributes.Count > 0)
{
foreach (var attribute in _selectedFeature.Attributes) {
// 該当フィールドのエイリアスを取得する(attribute.Key はフィールド名)
string fieldName = fields[attribute.Key];
// 該当フィールドのフィールド値を取得する
object fieldValue = attribute.Value!;
attributes.AppendLine(fieldName + ": " + fieldValue);
}
attributes.AppendLine();
}
// コールアウトのコンテンツを定義する CalloutDefinition を作成する
calloutDefinition = new CalloutDefinition(layerName, attributes.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "エラー");
}
return calloutDefinition!;
}
Tips: 選択したフィーチャをハイライトしたいときには FeatureLayer.SelectFeature()を使用します。このメソッドに渡されたフィーチャがハイライト表示されます。
5. アプリを実行して任意の地点をクリックしてみましょう。クリック地点上にフィーチャがある場合、コールアウトが表示され属性情報を確認できます。
第5弾「個別属性表示」ではマップに追加されているデータの属性情報を表示する方法を紹介しました。次回、第6弾ではデバイスの現在地をマップ上に表示する方法を紹介します。
関連リンク