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

Before starting this tutorial:

  1. You need an ArcGIS Location Platform or ArcGIS Online account.

  2. Confirm that your system meets the system requirements.

  3. An IDE for Android development in Kotlin.

Steps

In this tutorial, you will first code, build, and install the app on a device, and then add the MahouRivieraTrails.mmpk.

Open an Android Studio project

  1. To start this tutorial, complete the Display a map tutorial. Or download and unzip the Display a map solution in a new folder.

  2. Modify the old project for use in this new tutorial. Expand More info for instructions.

Add import statements

  1. Replace app-specific import statements with the imports needed for this tutorial.

    MainActivity.kt
    Use dark colors for code blocks
    17 18 19 20 21 22 23 24 25 26 27 28 29
    Change lineChange lineChange lineChange lineChange lineChange line
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    package com.example.app
    
    import android.os.Bundle
    import androidx.appcompat.app.AppCompatActivity
    
    import android.util.Log
    import android.widget.Toast
    import com.esri.arcgisruntime.ArcGISRuntimeEnvironment
    import com.esri.arcgisruntime.loadable.LoadStatus
    import com.esri.arcgisruntime.mapping.MobileMapPackage
    import com.esri.arcgisruntime.mapping.view.MapView
    
    import com.example.app.databinding.ActivityMainBinding
    

Open the mobile map package and display a map

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

  1. In Android Studio, in the Android tool window, open Gradle Scripts > build.gradle (Module:Display_a_map_from_a_mobile_map_package.app). Make sure your file matches the one below. In particular, verify that you have the buildFeatures and packagingOptions blocks shown.

    build.gradle (Module:Display_a_map_from_a_mobile_map_package.app)
    Expand
    Use dark colors for code blocksCopy
    29 30 31 32 33 34 35 36 37 38 39 40 41
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
        kotlinOptions {
            jvmTarget = JavaVersion.VERSION_1_8.toString()
        }
    
        buildFeatures {
            viewBinding true
        }
    
        packagingOptions {
            exclude 'META-INF/DEPENDENCIES'
        }
    
    }
    
    Expand
  2. In the Android tool window, open app > res > values > strings.xml. Add a string resource for MahouRivieraTrials.mmpk.

    strings.xml
    Use dark colors for code blocks
    1 2 3 4 5 6 7
    Add line.
    1
    2
    3
    4
    5
    6
    7
    <resources>
    
        <string name="app_name">Display a map from a mobile map package</string>
    
        <string name="mahourivieratrails_mmpk">/MahouRivieraTrails.mmpk</string>
    
    </resources>
  3. In the MainActivity() function, create a read-only property named TAG for the logger, and create a late-initializing property named mapPackage of type MobileMapPackage

    MainActivity.kt
    Use dark colors for code blocks
    31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
    Add line.Add line.Add line.Add line.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    
    class MainActivity : AppCompatActivity() {
    
        private val activityMainBinding by lazy {
          ActivityMainBinding.inflate(layoutInflater)
        }
    
        private val mapView: MapView by lazy {
          activityMainBinding.mapView
        }
    
        val TAG: String = MainActivity::class.java.simpleName
    
        private lateinit var mapPackage: MobileMapPackage
    
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
    
  4. In the onCreate() function, replace the setupMap() call with a loadMobileMapPackage() call.

    MainActivity.kt
    Use dark colors for code blocks
    47 48 49 50 51 52 53 54 55 56 57
    Change lineChange line
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
    
            setContentView(activityMainBinding.root)
    
            setApiKeyForApp()
    
            // access the mobile map package from the filesystem and load it into a MapView
            loadMobileMapPackage(getExternalFilesDir(null)?.path + getString(R.string.mahourivieratrails_mmpk))
    
        }
    
  5. Find the setUpMap() function and delete it.

    MainActivity.kt
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
        // set up your map here. You will call this method from onCreate()
        private fun setupMap() {
    
            // create a map with the BasemapStyle streets
            val map = ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC)
    
            // set the map to be displayed in the layout's MapView
            mapView.map = map
    
            // set the viewpoint, Viewpoint(latitude, longitude, scale)
            mapView.setViewpoint(Viewpoint(34.0270, -118.8050, 72000.0))
    
        }
    
  6. Create a loadlMobileMapPackage() function that takes the .mmpk's path and file name. In the function, create the MobileMapPackage and load the package.

    MainActivity.kt
    Expand
    Use dark colors for code blocks
    80 81 82 83 84 85 86 87 83
    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
        // load your mobile map package here. You call this method from onCreate()
        private fun loadMobileMapPackage(mmpkFilePath: String) {
    
            // create the mobile map package
            mapPackage = MobileMapPackage(mmpkFilePath)
            // load the mobile map package
            mapPackage.loadAsync()
    
        } // end of loadMobileMapPackage()
    
    Expand
  7. Add a done loading listener, and pass it a lambda that will be invoked when the mobile map package has loaded. Then check whether the mobile map package has loaded and if it has any maps. Finally, add the first map from the mobile map package to the MapView.

    MainActivity.kt
    Expand
    Use dark colors for code blocks
    80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
        // load your mobile map package here. You call this method from onCreate()
        private fun loadMobileMapPackage(mmpkFilePath: String) {
    
            // create the mobile map package
            mapPackage = MobileMapPackage(mmpkFilePath)
            // load the mobile map package
            mapPackage.loadAsync()
    
            // add done listener which will invoke when mobile map package has loaded
            mapPackage.addDoneLoadingListener() {
                // check load status and that the mobile map package has maps
                if (mapPackage.loadStatus === LoadStatus.LOADED && mapPackage.maps.isNotEmpty()) {
                    // add the map from the mobile map package to the MapView
                    mapView.map = mapPackage.maps[0]
                }
    
            }
    
        } // end of loadMobileMapPackage()
    
    Expand
  8. Add a call to logError() for cases where the mobile map package status is not loaded or the package has no maps in it.

    MainActivity.kt
    Expand
    Use dark colors for code blocks
    86 87 88 89 90 91 92 93 94 95 96 97 98 99
    Add line.Add line.Add line.Add line.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
            // add done listener which will invoke when mobile map package has loaded
            mapPackage.addDoneLoadingListener() {
                // check load status and that the mobile map package has maps
                if (mapPackage.loadStatus === LoadStatus.LOADED && mapPackage.maps.isNotEmpty()) {
                    // add the map from the mobile map package to the MapView
                    mapView.map = mapPackage.maps[0]
                }
    
                else {
                    // log an error if the mobile map package fails to load
                    logError(mapPackage.loadError.message)
                }
    
            }
    
    Expand
  9. Create a logError() function that logs errors to logcat and to the screen via Toast. The parameter passed to the function will be logged as the message.

    MainActivity.kt
    Expand
    Use dark colors for code blocks
    103 104 105 106 107 108 109 110
    Add line.Add line.Add line.Add line.Add line.Add line.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
        } // end of loadMobileMapPackage()
    
        private fun logError(message: String?) {
            message?.let {
                Log.e(TAG, it)
                Toast.makeText(this, it, Toast.LENGTH_LONG).show()
            }
        }
    
    Expand

Build and install your app

This is a necessary step, because the app install process creates the path (sdcard > Android > data > com.example.app > files) where you will upload your mobile map package to the device. Do not try to create the path manually in Device File Explorer, as you will get a Operation not permitted error.

  1. Click Run > Run > app to run the app.

    Your app will be built and installed on the Android Virtual Device (AVD) that is currently selected in the Android Studio toolbar. On the AVD, you should see a blank window titled Display a map from a mobile map package, with the message File not found. displaying briefly at the bottom. This is expected.

    Next you will add the mobile map package.

Add a mobile map package using Device File Explorer

Add a mobile map package (.mmpk) to the device file system for use by your app.

  1. Create or download the MahouRivieraTrails.mmpk mobile map package to your development computer. Either complete the Create a mobile map package tutorial to create the package yourself, or download the MahouRivieraTrails.mmpk mobile map package.

  2. In Android Studio, verify that your Android Virtual Device (AVD) is still connected. If it is not, expand More info below.

  3. Display the Device File Explorer tool window, which shows the file system on your AVD. Click View > Tool Windows > Device File Explorer and wait until Device File Manager connects to your AVD and shows the file system tree.

  4. Upload the MahouRivieraTrails.mmpk file from your development computer.

    1. In Device File Manager, right-click on the sdcard > Android > data directory and click Synchronize.

    2. Right-click on sdcard > Android > data > com.example.app > files and click Upload. Navigate to the MahouRivieraTrails.mmpk file on your development computer. Then click OK.

      You should see a Device File Explorer Toast in the lower corner confirming success.

Restart the app on your AVD

  1. On your AVD, shut down your app. On many recent Android devices, you shut down an app by tapping the square icon and swiping up on the app window.

  2. Then go to the All Apps screen and start the app again by clicking the Display a mobile map package app icon.

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

What's next?

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

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.