Display a map from a mobile map package

Learn how to display a map from a mobile map package (MMPK).

display a map from a mobile map package

In this tutorial you will display a fully interactive map from a mobile map package (MMPK). The map contains a basemap layer and data layers and does not require a network connection.

Prerequisites

The following are required for this tutorial:

  1. An ArcGIS account to access your API keys. If you don't have an account, sign up for free.
  2. Your system meets the system requirements.
  3. The ArcGIS Runtime API for iOS is installed.

Steps

Open an Xcode project

  1. To start the tutorial, complete the Display a map tutorial or download and unzip the solution.

  2. Open the .xcodeproj file in Xcode.

Add a mobile map package

You will add a mobile map package (MMPK) to your Xcode project. To get an .mmpk file, you can complete the Create a mobile map package tutorial or download its solution.

Once you have the mobile map package:

  1. In Xcode's app menu, click File > Add Files to "...".
  2. Navigate to and select the .mmpk file. If you used the tutorial solution, it will be named MahouRivieraTrails.mmpk.
  3. Choose Options, select the Copy items if needed checkbox, and ensure the project target is selected in Add to targets. Click Add.

Open the mobile map package and display a map

Select a map from the maps contained in a mobile map package, and display it in a map view. Use the AGSMobileMapPackage class to access the mobile map package, and load it to read its contents.

  1. In Xcode, in the Project Navigator, click ViewController.swift.

  2. In the editor, modify the setupMap() function to create an AGSMobileMapPackage referencing the mobile map package you added to the project.

    ViewController.swift
    Change lineChange lineChange line
    23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 24 25 26 27 28 29 29 29 29 29 29 29 29 29 29 29 29 30 30 30 30 30 30 30 30 30 30
    // 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.
    
    
    import UIKit
    
    import ArcGIS
    
    class ViewController: UIViewController {
    
        @IBOutlet weak var mapView: AGSMapView!
    
        private func setupMap() {
    
            let mobileMapPackage = AGSMobileMapPackage(
                name: "MahouRivieraTrails"
            )
    
            mobileMapPackage.load { [weak self] error in
                if let error = error {
                    print("Error loading the mmpk: \(error.localizedDescription)")
                    return
                }
    
                if let map = mobileMapPackage.maps.first {
                    self?.mapView.map = map
                }
            }
    
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            setupMap()
    
        }
    
    }
    

    The file name must match exactly, but the file extension is not required. The name reflects the MahouRivieraTrails.mmpk added to the bundle.

    If you sideload or download the .mmpk file, you should provide the full file URL to the file's location on the device.

  3. Load the AGSMobileMapPackage and check that it loads without error. Display the first map in the package in the map view.

    ViewController.swift
    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.
    23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 41 41 41 41 41 41 41 41 41
    // 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.
    
    
    import UIKit
    
    import ArcGIS
    
    class ViewController: UIViewController {
    
        @IBOutlet weak var mapView: AGSMapView!
    
        private func setupMap() {
    
            let mobileMapPackage = AGSMobileMapPackage(
                name: "MahouRivieraTrails"
            )
    
            mobileMapPackage.load { [weak self] error in
                if let error = error {
                    print("Error loading the mmpk: \(error.localizedDescription)")
                    return
                }
    
                if let map = mobileMapPackage.maps.first {
                    self?.mapView.map = map
                }
            }
    
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            setupMap()
    
        }
    
    }
    

    An AGSMobileMapPackage can contain many maps in a maps array.

    Loading the mobile map package is an asynchronous process, and the Runtime API takes care of reading the file on a thread that will not block the UI.

  4. Press <Command+R> to run the app.

    If you are using the Xcode simulator your system must meet these minimum requirements: macOS Catalina, Xcode 11, iOS 13. If you are using a physical device, then refer to the system requirements.

You should see a map of trail heads, trails, and parks for the area south of the Santa Monica mountains. You will be able to pinch (to zoom and rotate), drag, and double-tap the map view to explore the map.

What's next?