The "Desktop Map App Development" seriesPart3 article.
Part3,Part2 created in the Visual Studio project, we implement the function to display the Web Map created in Part 1.
Implementing Map Display
Part2 created in the Visual Studio project, we will implement the function to display a Web map. Sample code for the features introduced in this article is also published on Esri Japan GitHub.
1. Replace the MapViewModel() in MapViewModel.cs as follows.
MapViewModel.cs
public MapViewModel()
{
_ = SetupMap();
}
2. Add the SetupMap method to the MapViewModel class.Specify the Web map's ID as an argument to the PortalItem.CreateAsync() method. Here, a sample Web map's ID is specified, but if you create your own Web map, please replace the ID accordingly.
MapViewModel.cs
private async Task SetupMap()
{
// Get the map using the Web map's ID
ArcGISPortal portal = await ArcGISPortal.CreateAsync();
PortalItem mapItem = await PortalItem.CreateAsync(portal, "3cbd757aede647869515251231a27e08");
// Assign the Web map to the Map property of MapView
Map = new Map(mapItem);
// Load the map
await Map.LoadAsync();
}
3. To access ArcGIS Platform base maps and geocode services (used in later steps), you need to set an API key.For information on the role of API keys and how to create them, please refer to "API Key Acquisition."
App.xaml.cs
ArcGISRuntimeEnvironment.Initialize(config => config
// .UseLicense("[Your ArcGIS Maps SDK License key]")
.UseApiKey("[Set your acquired API key here]")
.ConfigureAuthentication(auth => auth
.UseDefaultChallengeHandler() // Use the default authentication dialog
4. Run the app and check if the map is displayed. At this point, place names on the base map are displayed in English.
5. Add processing to localize place names on the base map using the following code.BasemapStyleParameters を使用することで、ベースマップの地名ラベルを指定した特定の言語や、アプリを実行する OS のロケールの言語で表示することができます。
MapViewModel.cs
Map = new Map(mapItem);
// ベースマップ スタイルのパラメーターを作成する
var basemapStyleParameters = new BasemapStyleParameters()
{
// ベースマップの地名ラベルを OS のロケールの言語で表示する
LanguageStrategy = BasemapStyleLanguageStrategy.ApplicationLocale
};
// ベースマップのタイプ(ArcGISTopographic:地形図)とベースマップ スタイルのパラメーターを指定して、新しい Basemap オブジェクトを作成する
var localizedBasemap = new Basemap(BasemapStyle.ArcGISTopographic, basemapStyleParameters);
// Map の Basemap オブジェクトを新しく作成した Basemap オブジェクトに置き換える
Map.Basemap = localizedBasemap;
await Map.LoadAsync();
6. 再度アプリを実行して、マップが表示されるか確認します。ベースマップの地名ラベルが日本語(アプリを実行する OS のロケールの言語)で表示されています。
Tips:ベースマップのタイプ(BasemapStyle)は、地形図(ArcGISTopographic)の他にも、道路地図(ArcGISStreets)、衛星画像(ArcGISImagery)、OpenStreetMap(OSMStandard)なども使用できます。
第3弾「マップ表示」では Web マップを表示する方法を紹介してきました。次回、第4弾ではマップ操作機能の実装方法を紹介します。
関連リンク