FAQ
ArcObjects でラベルを表示したい

ナレッジ番号:3385 | 登録日:2023/05/29 | 更新日:2023/08/31

概要

ArcObjects でラベリングを行うためのサンプル コードです。???????

  • ラベルの表示を制御するには IGeoFeatureLayer::DisplayAnnotation プロパティを設定します。
  • ラベル クラスを追加したりラベル クラスの設定を変更することでラベルの設定ができますが、ILabelEngineLayerProperties2 インターフェイスのオブジェクトを生成することでラベル クラスの追加ができます。そのオブジェクトのプロパティを変更することでラベル クラスに対する設定を変更することができます。

サンプル コード 1

新規にラベル クラスを作成する例です。

//マップの最上位レイヤーを取得
ILayer pLayer = pMap.get_Layer(0);

//Layer オブジェクトからIGeoFeatureLayer インターフェイスのオブジェクトを作成
IGeoFeatureLayer pGeoFeatureLayer = (IGeoFeatureLayer)pLayer;
//フィーチャ レイヤーに用意されているラベル クラスを取得
IAnnotateLayerPropertiesCollection pAnnotateLayerPropertiesCollection = pGeoFeatureLayer.AnnotationProperties;

//ラベルクラスを新規作成
ILabelEngineLayerProperties2 pLabelEngineLayerProperties2 = new LabelEngineLayerPropertiesClass();
IAnnotateLayerProperties pAnnotateLayerProperties = (IAnnotateLayerProperties)pLabelEngineLayerProperties2;
//ラベル クラスを新規に作成
pAnnotateLayerProperties.Class = "新規ラベルクラス";
pAnnotateLayerProperties.FeatureLayer = (IFeatureLayer)pLayer;
//ラベルを表示させたいフィーチャを指定するための SQL 文
pAnnotateLayerProperties.WhereClause = "KEN = '大阪府'";

//ラベルとして表示する属性 (条件式) を設定 
ILabelEngineLayerProperties pLabelEngineLayerProperties = (ILabelEngineLayerProperties)pLabelEngineLayerProperties2;
pLabelEngineLayerProperties.Expression = "[SIKUCHOSON]";

//作成したラベル クラスを追加
pAnnotateLayerPropertiesCollection.Add(pAnnotateLayerProperties);

//ラベルの表示
pGeoFeatureLayer.DisplayAnnotation = true;

//ビューの更新
ArcMap.Document.ActiveView.Refresh();

サンプル コード 2

既存のラベル クラスを変更する例です。

//マップの最上位レイヤーを取得
ILayer pLayer = pMap.get_Layer(0);

//Layer オブジェクトからIGeoFeatureLayer インターフェイスのオブジェクトを作成
IGeoFeatureLayer pGeoFeatureLayer = (IGeoFeatureLayer)pLayer;
//フィーチャ レイヤーに用意されているラベル クラスの取得
IAnnotateLayerPropertiesCollection pAnnotateLayerPropertiesCollection = pGeoFeatureLayer.AnnotationProperties;

// 一つ目(インデックス番号 0)のラベル クラスを取得(初期設定では「Default」という名前のラベル クラス)
IAnnotateLayerProperties pAnnotateLayerProperties = null;
IElementCollection pElementCollection = null;
IElementCollection pUnplacedCollection = null;
pAnnotateLayerPropertiesCollection.QueryItem(0, out pAnnotateLayerProperties, out pElementCollection, out pUnplacedCollection);
ILabelEngineLayerProperties pLabelEngineLayerProperties = pAnnotateLayerProperties as ILabelEngineLayerProperties;
pLabelEngineLayerProperties.Expression = "[SIKUCHOSON]";
//ラベルを表示させたいフィーチャを選択する SQL 文を定義
pAnnotateLayerProperties.WhereClause = "KEN = '大阪府'";

//ラベルの表示
pGeoFeatureLayer.DisplayAnnotation = true;

//ビューの更新
ArcMap.Document.ActiveView.Refresh();

参考ドキュメント

Working with labels and annotation

メタデータ

種類

製品