レイヤーの追加
Map Appで何かしらの情報を表示するためには,
Layerクラスは,
namespace HelloWorldBingMapApp
{
using Microsoft.Maps.Core;
using Microsoft.Maps.Plugins;
public class MainLayer : Layer
{
public MainLayer(PluginToken pluginToken) : base(pluginToken)
{
}
}
}
今回はレイヤーで何も処理しないため,
HelloWorldPluginクラスに戻り,
このメソッド内でプラグインに既にレイヤーが追加されていればレイヤーを表示,
private MainLayer mainLayer;
[ImportSingle("Microsoft/LayerManagerContract", ImportLoadPolicy.Synchronous)]
public LayerManagerContract LayerManagerContract { get; set; }
public override void Activate(System.Collections.Generic.IDictionary<string, string> activationParameters)
{
if (LayerManagerContract.ContainsLayer(this.mainLayer))
{
LayerManagerContract.BringToFront(this.mainLayer);
}
else
{
LayerManagerContract.AddLayer(this.mainLayer);
}
}
上記コードでは,
プッシュピンの追加
続いてレイヤーの中にプッシュピンを追加してみましょう。今回作成するMap Appではユーザーが表示している地図上にひとつのプッシュピンを表示します。また,
表示されている地図の操作,
[ImportSingle("Microsoft/MapContract", ImportLoadPolicy.Synchronous)]
public MapContract DefaultMap { get; set; }
[ImportSingle("Microsoft/PushpinFactoryContract", ImportLoadPolicy.Synchronous)]
public PushpinFactoryContract PushpinFactoryContract { get; set; }
[ImportSingle("Microsoft/PopupContract", ImportLoadPolicy.Synchronous)]
public PopupContract PopupContract { get; set; }
プッシュピンを作成するには,
Initializeメソッドの内容を変更したコードを示します。先ほどのレイヤーのインスタンス生成もここで行います。
public override void Initialize()
{
base.Initialize();
this.mainLayer = new MainLayer(this.Token);
var pin = PushpinFactoryContract.CreateStandardPushpin(this.DefaultMap.Center);
var entity = new Entity(pin);
this.mainLayer.Entities.Add(entity);
}
CreateStandardPushpinメソッドによる戻り値はレイヤーに直接追加できる型でありません。そのためEntityオブジェクトを生成しPrimitiveプロパティに値を設定しています。エンティティは地図上に表示するアイテムで,
最後にプッシュピンクリック時の処理です。PopupContractのRegisterメソッドを用いて次のように記述できます。Initializeメソッド内のレイヤーに追加する前に追記します。
PopupContract.Register(
entity,
(PopupStateChangeContext context) =>
{
context.Title = "こんにちは!";
if (context.State == PopupState.Normal)
{
context.Content = pin.Location.ToString();
}
});
ポップアップを表示するエンティティとポップアップの状態が変更されたときの処理を指定しています。