Learn how to display a map

In this tutorial you will display a fully interactive map
Prerequisites
Before starting this tutorial:
-
You need an ArcGIS Location Platform or ArcGIS Online account.
-
Ensure your development environment meets the system requirements.
Optionally, you may want to install the ArcGIS Maps SDK for .NET to get access to project templates in Visual Studio (Windows only) and offline copies of the NuGet packages.
Steps
Open a Visual Studio solution
- To start the tutorial, complete the Display a map tutorial or download and unzip the solution.
- Open the
.slnfile in Visual Studio.
If you are developing with Visual Studio for Windows, ArcGIS Maps SDK for .NET provides a set of project templates for each supported .NET platform. These templates follow the Model-View-ViewModel (MVVM) design pattern. Install the ArcGIS Maps SDK for .NET Visual Studio Extension to add the templates to Visual Studio (Windows only). See Install and set up for details.
Update the tutorial name used in the project (optional)
The Visual Studio solution, project, and the namespace for all classes currently use the name DisplayAMap. Follow the steps below if you prefer the name to reflect the current tutorial. These steps are not required, your code will still work if you keep the original name.
The tutorial instructions and code use the name DisplayAMapFromAMobileMapPackage for the solution, project, and namespace. You can choose any name you like, but it should be the same for each of these.
-
Update the name for the solution and the project.
- In Visual Studio, in the Solution Explorer, right-click the solution name and choose Rename. Provide the new name for your solution.
- In the Solution Explorer, right-click the project name and choose Rename. Provide the new name for your project.
-
Rename the namespace used by classes in the project.
- In the Solution Explorer, expand the project node.
- Double-click MapViewModel.cs in the Solution Explorer to open the file.
- In the
MapViewModelclass, double-click the namespace name (DisplayAMap) to select it, and then right-click and choose Rename…. - Provide the new name for the namespace.
- Click Apply in the Rename: DisplayAMap window that appears in the upper-right of the code window. This will rename the namespace throughout your project.
-
Build the project.
- Choose Build > Build solution (or press <F6>).
Add a mobile map package to the project
Add a mobile map package (.mmpk) for distribution with your app.
-
Create or download the
MahouRivieraTrails.mmpkmobile map package. Complete the Create a mobile map package tutorial to create the package yourself. Otherwise, download the solution file: MahouRivieraTrails.mmpk. -
From the Visual Studio Project menu, choose Add > Existing item …
-
Navigate to the folder that contains your mobile map package. Change the file type in the dialog to
All files (*.*), chooseMahouRivieraTrails.mmpk, then click Add to add it to the project. -
In the Visual Studio Solution Explorer, select
MahouRivieraTrails.mmpk. In the Properties window, set the following properties of the file.- Build Action:
Content - Copy to Output Directory:
Copy always
- Build Action:
Open the mobile map package and display a map
Select a mapMobileMapPackage class to access the mobile map package and load it to read its contents.
-
In the Visual Studio > Solution Explorer, double-click MapViewModel.cs to open the file.
-
Add additional required
usingstatements at the top of the class.MapViewModel.csusing System;using System.Collections.Generic;using System.Text;using Esri.ArcGISRuntime.Geometry;using Esri.ArcGISRuntime.Mapping;using System.ComponentModel;using System.Runtime.CompilerServices;using System.Linq;using System.Threading.Tasks; -
In the MapViewModel class, remove all the existing code in the
SetupMap()function.MapViewModel.csprivate void SetupMap(){// Create a new map with a 'topographic vector' basemap.var map = new Map(BasemapStyle.ArcGISTopographic);// Set the initial viewpoint around the Santa Monica Mountains in California.var mapCenterPoint = new MapPoint(-118.805, 34.027, SpatialReferences.Wgs84);map.InitialViewpoint = new Viewpoint(mapCenterPoint, 100000);// Set the view model's Map property with the map.Map = map;} -
Modify the signature of the
SetupMap()function to include theasynckeyword and to returnTaskrather thanvoid.MapViewModel.cs57 collapsed lines// Copyright 2021 Esri// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at//// https://www.apache.org/licenses/LICENSE-2.0//// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.using System;using System.Collections.Generic;using System.Text;using Esri.ArcGISRuntime.Geometry;using Esri.ArcGISRuntime.Mapping;using System.ComponentModel;using System.Runtime.CompilerServices;using System.Linq;using System.Threading.Tasks;namespace DisplayAMapFromAMobileMapPackage{class MapViewModel : INotifyPropertyChanged{public MapViewModel(){_ = SetupMap();}public event PropertyChangedEventHandler? PropertyChanged;protected void OnPropertyChanged([CallerMemberName] string propertyName = ""){PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));}private Map? _map;public Map? Map{get { return _map; }set{_map = value;OnPropertyChanged();}}private async Task SetupMap(){}4 collapsed lines}}When calling methods asynchronously inside a function (using the
awaitkeyword), theasynckeyword is required in the signature.Although a
voidreturn type would continue to work, this is not considered best practice. Exceptions thrown by anasync voidmethod cannot be caught outside of that method, are difficult to test, and can cause serious side effects if the caller is not expecting them to be asynchronous. The only circumstance whereasync voidis acceptable is when using an event handler, such as a button click.See the Microsoft documentation for more information about Asynchronous programming with async and await.
-
(Optional) Modify the call to
SetupMap()(in theMapViewModelconstructor) to avoid a compilation warning. After changingSetupMap()to an asynchronous method, the following warning appears in the Visual Studio Error List.Because this call is not awaited, execution of the current method continues before the call iscompleted. Consider applying the 'await' operator to the result of the call.Because your code does not anticipate a return value from this call, the warning can be ignored. Your code will run despite the warning. But if you want to be more specific about your intentions with this call and to address the warning, add the following code to store the return value in a discard.
MapViewModel.cs32 collapsed lines// Copyright 2021 Esri// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at//// https://www.apache.org/licenses/LICENSE-2.0//// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.using System;using System.Collections.Generic;using System.Text;using Esri.ArcGISRuntime.Geometry;using Esri.ArcGISRuntime.Mapping;using System.ComponentModel;using System.Runtime.CompilerServices;using System.Linq;using System.Threading.Tasks;namespace DisplayAMapFromAMobileMapPackage{class MapViewModel : INotifyPropertyChanged{public MapViewModel(){_ = SetupMap();}39 collapsed linespublic event PropertyChangedEventHandler? PropertyChanged;protected void OnPropertyChanged([CallerMemberName] string propertyName = ""){PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));}private Map? _map;public Map? Map{get { return _map; }set{_map = value;OnPropertyChanged();}}private async Task SetupMap(){// Define the path to the mobile map package.string pathToMobileMapPackage = System.IO.Path.Combine(Environment.CurrentDirectory, @"MahouRivieraTrails.mmpk");// Instantiate a new mobile map package.MobileMapPackage mahouRivieraTrails_MobileMapPackage = new MobileMapPackage(pathToMobileMapPackage);// Load the mobile map package.await mahouRivieraTrails_MobileMapPackage.LoadAsync();// Show the first map in the mobile map package.this.Map = mahouRivieraTrails_MobileMapPackage.Maps.FirstOrDefault();}}}From the Microsoft documentation:
“[Discards] are placeholder variables that are intentionally unused in application code. Discards are equivalent to unassigned variables; they don’t have a value. A discard communicates intent to the compiler and others that read your code: You intended to ignore the result of an expression.”
-
Add the following code to the
SetupMap()function. This code defines the path to the mobile map package relative to the app, creates the mobile map package using theMobileMapPackageconstructor, loads the mobile map package asynchronously, and adds the first map in the mobile map package to theMapView.60 collapsed lines// Copyright 2021 Esri// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at//// https://www.apache.org/licenses/LICENSE-2.0//// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.using System;using System.Collections.Generic;using System.Text;using Esri.ArcGISRuntime.Geometry;using Esri.ArcGISRuntime.Mapping;using System.ComponentModel;using System.Runtime.CompilerServices;using System.Linq;using System.Threading.Tasks;namespace DisplayAMapFromAMobileMapPackage{class MapViewModel : INotifyPropertyChanged{public MapViewModel(){_ = SetupMap();}public event PropertyChangedEventHandler? PropertyChanged;protected void OnPropertyChanged([CallerMemberName] string propertyName = ""){PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));}private Map? _map;public Map? Map{get { return _map; }set{_map = value;OnPropertyChanged();}}private async Task SetupMap(){// Define the path to the mobile map package.string pathToMobileMapPackage = System.IO.Path.Combine(Environment.CurrentDirectory, @"MahouRivieraTrails.mmpk");// Instantiate a new mobile map package.MobileMapPackage mahouRivieraTrails_MobileMapPackage = new MobileMapPackage(pathToMobileMapPackage);// Load the mobile map package.await mahouRivieraTrails_MobileMapPackage.LoadAsync();// Show the first map in the mobile map package.this.Map = mahouRivieraTrails_MobileMapPackage.Maps.FirstOrDefault();6 collapsed lines}}}A
MobileMapPackagecan contain many maps in aMapsarray.Loading the mobile map package
A mobile map package (MMPK) is a standalone file that contains one or more map definitions, including the basemap layers, data layers, layer styles, and pop-up styles for use in offline applications built with ArcGIS Maps SDKs for Native Apps. is an asynchronous process. The file is read on a thread that does not block the UI. -
Click Debug > Start Debugging (or press <F5> on the keyboard) to run the app. If your app uses user authentication, enter your ArcGIS Online credentials when prompted.
If creating apps for Android or iOS, you will need the appropriate emulator, simulator, or device configured for testing (see System requirements for details)
You will see a map
What’s next?
Learn how to use additional API features, ArcGIS location services, and ArcGIS tools in these tutorials: