Learn how to display a map A map is a collection of layers that are displayed in 2D. It is typically composed of a basemap layer and data layers. Learn more from a mobile map package (MMPK) 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. Learn more .

display a map from a mobile map package

In this tutorial you will display a fully interactive map A map is a collection of layers that are displayed in 2D. It is typically composed of a basemap layer and data layers. Learn more from a mobile map package (MMPK) 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. Learn more . The map contains a basemap layer A basemap layer is the layer in a map or scene that displays basemap data. The data source for a basemap layer is typically a basemap service. Learn more and data layers A data layer is a layer that references geographic data from a file or a service and is used to visualize the data in a map or scene. Learn more and does not require a network connection.

This tutorial builds a cross-platform app using the .NET Multi-platform App UI (.NET MAUI) framework. .NET MAUI allows you to build native cross-platform apps from a shared code base. If you’d prefer to build this app as a Windows desktop app, see the Windows Presentation Foundation (WPF) version of this tutorial.

Prerequisites

Before starting this tutorial:

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

  1. To start the tutorial, complete the Display a map (.NET MAUI) tutorial or download and unzip the solution.
  2. Open the .sln file in Visual Studio.

ArcGIS Maps SDK for .NET supports apps for Windows Presentation Framework (WPF), Universal Windows Platform (UWP), Windows UI Library (WinUI), and Multi-platform App UI (.NET MAUI). The instructions for this tutorial are specific to creating a .NET MAUI project using Visual Studio for Windows.

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.

Add a mobile map package to the project

Add a mobile map package (.mmpk) for distribution with your app.

  1. Create or download the MahouRivieraTrails.mmpk mobile map package. Complete the Create a mobile map package tutorial to create the package yourself. Otherwise, download the solution file: MahouRivieraTrails.mmpk.

  2. Right-click the project name in the Visual Studio Solution Explorer and choose Add > New Folder. Name the folder Assets.

  3. Right-click on the new Assets folder and choose Add > Existing item … from the context menu.

  4. Navigate to the folder that contains your mobile map package. Change the file type in the dialog to All files (*.*), choose MahouRivieraTrails.mmpk, then click Add to add it to the project.

  5. In the Visual Studio Solution Explorer, select MahouRivieraTrails.mmpk. In the Properties window, set the following properties of the file.

    • Build Action: MauiAsset
    • Copy to Output Directory: Copy always

Open the mobile map package and display a map

Select a map A map is a collection of layers that are displayed in 2D. It is typically composed of a basemap layer and data layers. Learn more from the maps contained in a 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. Learn more and display it in a map view. Use the MobileMapPackage class to access the mobile map package and load it to read its contents.

  1. In the Visual Studio > Solution Explorer, double-click MapViewModel.cs to open the file.

  2. In the MapViewModel class, remove all the existing code in the SetupMap() function.

    MapViewModel.cs
    private void SetupMap()
    {
    // Create a new map with a 'topographic vector' basemap.
    Map = new Map(BasemapStyle.ArcGISTopographic);
    var mapCenterPoint = new MapPoint(-118.805, 34.027, SpatialReferences.Wgs84);
    Map.InitialViewpoint = new Viewpoint(mapCenterPoint, 100000);
    }
  3. Modify the signature of the SetupMap() function to include the async keyword and to return Task rather than void.

    MapViewModel.cs
    private async Task SetupMap()
    {
    }
  4. (Optional) Modify the call to SetupMap() (in the MapViewModel constructor) to avoid a compilation warning. After changing SetupMap() 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 is
    completed. 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.cs
    public MapViewModel()
    {
    _ = SetupMap();
    }
  5. Add the following code to the SetupMap() function. This code ensures that the mobile map package is included in the app’s AppDataDirectory, creates the mobile map package using the MobileMapPackage constructor, loads the mobile map package asynchronously, and adds the first map in the mobile map package to the MapView.

    private async Task SetupMap()
    {
    // In .NET MAUI bundled files are retrieved as a read-only stream. In order to load the mobile map package it needs to be written to the
    // AppDataDirectory from where it can then be loaded to initialize the mobile map package.
    string appDataDirectoryPathToMobileMapPackage = await GetAppDataDirectoryFilePath("Assets/MahouRivieraTrails.mmpk", "MahouRivieraTrails.mmpk.mmpk");
    // Instantiate a new mobile map package.
    MobileMapPackage mahouRivieraTrails_MobileMapPackage = new MobileMapPackage(appDataDirectoryPathToMobileMapPackage);
    // 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. Add a method that copies the mobile map package to the AppDataDirectory so that it can be accessed and read at runtime.

    73 collapsed lines
    // Copyright 2023 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.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Esri.ArcGISRuntime.Mapping;
    using Map = Esri.ArcGISRuntime.Mapping.Map;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using Esri.ArcGISRuntime.Geometry;
    namespace DisplayAMapFromAMobileMapPackage
    {
    internal 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 => _map;
    set
    {
    _map = value;
    OnPropertyChanged();
    }
    }
    private async Task SetupMap()
    {
    // In .NET MAUI bundled files are retrieved as a read-only stream. In order to load the mobile map package it needs to be written to the
    // AppDataDirectory from where it can then be loaded to initialize the mobile map package.
    string appDataDirectoryPathToMobileMapPackage = await GetAppDataDirectoryFilePath("Assets/MahouRivieraTrails.mmpk", "MahouRivieraTrails.mmpk.mmpk");
    // Instantiate a new mobile map package.
    MobileMapPackage mahouRivieraTrails_MobileMapPackage = new MobileMapPackage(appDataDirectoryPathToMobileMapPackage);
    // Load the mobile map package.
    await mahouRivieraTrails_MobileMapPackage.LoadAsync();
    // Show the first map in the mobile map package.
    this.Map = mahouRivieraTrails_MobileMapPackage.Maps.FirstOrDefault();
    }
    private async Task<string> GetAppDataDirectoryFilePath(string resourceFilePath, string fileName)
    {
    // Check to see if the mobile map package is already present in the AppDataDirectory.
    if (File.Exists(Path.Combine(FileSystem.Current.AppDataDirectory, fileName)))
    {
    // If the mobile map package is present in the AppDataDirectory, return the file path.
    return Path.Combine(FileSystem.Current.AppDataDirectory, fileName);
    }
    // Open the source file.
    using Stream inputStream = await FileSystem.OpenAppPackageFileAsync(resourceFilePath);
    // Create an output filename
    string targetFile = Path.Combine(FileSystem.Current.AppDataDirectory, fileName);
    // Copy the file to the AppDataDirectory
    using FileStream outputStream = File.Create(targetFile);
    await inputStream.CopyToAsync(outputStream);
    return targetFile;
    }
    3 collapsed lines
    }
    }
  7. 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.

Deploy the app to a Windows, Mac, iOS, or Android device. You will see a map A map is a collection of layers that are displayed in 2D. It is typically composed of a basemap layer and data layers. Learn more of trailheads, trails, and parks for the area south of the Santa Monica mountains. Pan and zoom with mouse or touchscreen controls to explore the map.

What’s next?

Learn how to use additional API features, ArcGIS location services, and ArcGIS tools in these tutorials: