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 API keys. If you don't have an account, sign up for free.
  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.

    1. On your file system, delete the .idea folder, if present, at the top level of your project.

    2. In the Android tool window, open app > res > values > strings.xml.

      In the <string name="app_name"> element, change the text content to Display a map from a mobile map package.

      strings.xml
             
      Change 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 Android tool window, open Gradle Scripts > settings.gradle.

      Change the value of rootProject.name to "Display a map from a mobile map package".

      settings.gradle
         
      Change line
      1
      2
      3
      include ':app'
      
      rootProject.name = "Display a map from a mobile map package"
    4. Click File > Sync Project with Gradle files. Android Studio will recognize your changes and create a new .idea folder.

  3. If you downloaded the Display a map solution project, get your API key and set it in your app.

    An API Key enables access to services, web maps, and web scenes hosted in ArcGIS Online.

    1. Go to your developer dashboard to get your API key. For these tutorials, use your default API key. It is scoped to include all of the services demonstrated in the tutorials.

    2. In Android Studio: in the Android tool window, open app > java > com.example.app > MainActivity.

    3. In the setApiKeyForApp() method, find the ArcGISRuntimeEnvironment.setApiKey("YOUR_API_KEY") call and paste your API key inside the quotes, replacing YOUR_API_KEY.

      MainActivity.kt
                                                                                               
      Change 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
      /*
       * Copyright 2020 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.
       */
      
      package com.example.app
      
      import android.os.Bundle
      import androidx.appcompat.app.AppCompatActivity
      
      import com.esri.arcgisruntime.ArcGISRuntimeEnvironment
      import com.esri.arcgisruntime.mapping.ArcGISMap
      import com.esri.arcgisruntime.mapping.BasemapStyle
      import com.esri.arcgisruntime.mapping.Viewpoint
      import com.esri.arcgisruntime.mapping.view.MapView
      
      import com.example.app.databinding.ActivityMainBinding
      
      class MainActivity : AppCompatActivity() {
      
          private val activityMainBinding by lazy {
            ActivityMainBinding.inflate(layoutInflater)
          }
      
          private val mapView: MapView by lazy {
            activityMainBinding.mapView
          }
      
          override fun onCreate(savedInstanceState: Bundle?) {
              super.onCreate(savedInstanceState)
      
              setContentView(activityMainBinding.root)
      
              setApiKeyForApp()
      
              setupMap()
      
          }
      
          override fun onPause() {
              mapView.pause()
              super.onPause()
          }
      
          override fun onResume() {
              super.onResume()
              mapView.resume()
          }
      
          override fun onDestroy() {
              mapView.dispose()
              super.onDestroy()
          }
      
          private fun setApiKeyForApp(){
              // set your API key
              // Note: it is not best practice to store API keys in source code. The API key is referenced
              // here for the convenience of this tutorial.
      
              ArcGISRuntimeEnvironment.setApiKey("YOUR_API_KEY")
      
          }
      
          // 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))
      
          }
      
      }
      

Add import statements

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

    MainActivity.kt
    16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 17 18 19 20 21 22 23 24 25 26 27 28 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 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
    113
    114
    115
    /*
     * Copyright 2020 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.
     */
    
    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
    
    
    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)
    
            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))
    
        }
    
        override fun onPause() {
            mapView.pause()
            super.onPause()
        }
    
        override fun onResume() {
            super.onResume()
            mapView.resume()
        }
    
        override fun onDestroy() {
            mapView.dispose()
            super.onDestroy()
        }
    
        private fun setApiKeyForApp(){
            // set your API key
            // Note: it is not best practice to store API keys in source code. The API key is referenced
            // here for the convenience of this tutorial.
    
            ArcGISRuntimeEnvironment.setApiKey("YOUR_API_KEY")
    
        }
    
        // 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]
                }
    
                else {
                    // log an error if the mobile map package fails to load
                    logError(mapPackage.loadError.message)
                }
    
            }
    
        } // end of loadMobileMapPackage()
    
        private fun logError(message: String?) {
            message?.let {
                Log.e(TAG, it)
                Toast.makeText(this, it, Toast.LENGTH_LONG).show()
            }
        }
    
    }
    

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). In the android block, add a packagingOptions block that specifies certain excludes.

    build.gradle (Module:Display_a_map_from_a_mobile_map_package.app)
    29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 30 31 32 33 34 35 36 37 38 39 39 39 39 39 39 39 39 39 39 39
    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
    apply plugin: 'com.android.application'
    apply plugin: 'kotlin-android'
    
    android {
        compileSdkVersion 29
    
        defaultConfig {
            applicationId "com.example.app"
            minSdkVersion 23
            targetSdkVersion 29
            versionCode 1
            versionName "1.0"
        }
    
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            }
        }
    
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
    
        kotlinOptions {
            jvmTarget = JavaVersion.VERSION_1_8.toString()
    
        }
        buildFeatures {
            viewBinding true
        }
    
        packagingOptions {
            exclude 'META-INF/DEPENDENCIES'
        }
    
    } // close of android block
    
    dependencies {
        implementation fileTree(dir: "libs", include: ["*.jar"])
        implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
        implementation 'androidx.appcompat:appcompat:1.2.0'
        implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    
        implementation 'com.esri.arcgisruntime:arcgis-android:100.12.0'
    
    }
    
  2. In the Android tool window, open app > res > values > strings.xml. Add a string resource for MahouRivieraTrials.mmpk.

    strings.xml
    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
    30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 48 48 48 48 48 47 46 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 44 43 42 40 38 36 34 32 30 28 26 24 22 20 18 16 14 12 10 8 6 4 2 1 0 -1 -2 -3 -4 -5 -6 -7 -7
    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
    113
    114
    115
    /*
     * Copyright 2020 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.
     */
    
    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
    
    
    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)
    
            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))
    
        }
    
        override fun onPause() {
            mapView.pause()
            super.onPause()
        }
    
        override fun onResume() {
            super.onResume()
            mapView.resume()
        }
    
        override fun onDestroy() {
            mapView.dispose()
            super.onDestroy()
        }
    
        private fun setApiKeyForApp(){
            // set your API key
            // Note: it is not best practice to store API keys in source code. The API key is referenced
            // here for the convenience of this tutorial.
    
            ArcGISRuntimeEnvironment.setApiKey("YOUR_API_KEY")
    
        }
    
        // 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]
                }
    
                else {
                    // log an error if the mobile map package fails to load
                    logError(mapPackage.loadError.message)
                }
    
            }
    
        } // end of loadMobileMapPackage()
    
        private fun logError(message: String?) {
            message?.let {
                Log.e(TAG, it)
                Toast.makeText(this, it, Toast.LENGTH_LONG).show()
            }
        }
    
    }
    
  4. In the onCreate() function, replace the setupMap() call with a loadMobileMapPackage() call.

    MainActivity.kt
    46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 47 48 49 50 51 52 53 54 55 56 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 56 55 54 53 52 51 50 49 47 45 43 41 39 37 35 33 31 29 27 25 23 21 19 18 17 16 15 14 13 12 11 10 10
    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
    113
    114
    115
    /*
     * Copyright 2020 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.
     */
    
    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
    
    
    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)
    
            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))
    
        }
    
        override fun onPause() {
            mapView.pause()
            super.onPause()
        }
    
        override fun onResume() {
            super.onResume()
            mapView.resume()
        }
    
        override fun onDestroy() {
            mapView.dispose()
            super.onDestroy()
        }
    
        private fun setApiKeyForApp(){
            // set your API key
            // Note: it is not best practice to store API keys in source code. The API key is referenced
            // here for the convenience of this tutorial.
    
            ArcGISRuntimeEnvironment.setApiKey("YOUR_API_KEY")
    
        }
    
        // 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]
                }
    
                else {
                    // log an error if the mobile map package fails to load
                    logError(mapPackage.loadError.message)
                }
    
            }
    
        } // end of loadMobileMapPackage()
    
        private fun logError(message: String?) {
            message?.let {
                Log.e(TAG, it)
                Toast.makeText(this, it, Toast.LENGTH_LONG).show()
            }
        }
    
    }
    
  5. Find the setUpMap() function and delete it.

    MainActivity.kt
                                                                                             
    Remove lineRemove lineRemove lineRemove lineRemove lineRemove lineRemove lineRemove lineRemove lineRemove lineRemove lineRemove lineRemove 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
    /*
     * Copyright 2020 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.
     */
    
    package com.example.app
    
    import android.os.Bundle
    import androidx.appcompat.app.AppCompatActivity
    
    import com.esri.arcgisruntime.ArcGISRuntimeEnvironment
    import com.esri.arcgisruntime.mapping.ArcGISMap
    import com.esri.arcgisruntime.mapping.BasemapStyle
    import com.esri.arcgisruntime.mapping.Viewpoint
    import com.esri.arcgisruntime.mapping.view.MapView
    
    import com.example.app.databinding.ActivityMainBinding
    
    class MainActivity : AppCompatActivity() {
    
        private val activityMainBinding by lazy {
          ActivityMainBinding.inflate(layoutInflater)
        }
    
        private val mapView: MapView by lazy {
          activityMainBinding.mapView
        }
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
    
            setContentView(activityMainBinding.root)
    
            setApiKeyForApp()
    
            setupMap()
    
        }
    
        override fun onPause() {
            mapView.pause()
            super.onPause()
        }
    
        override fun onResume() {
            super.onResume()
            mapView.resume()
        }
    
        override fun onDestroy() {
            mapView.dispose()
            super.onDestroy()
        }
    
        private fun setApiKeyForApp(){
            // set your API key
            // Note: it is not best practice to store API keys in source code. The API key is referenced
            // here for the convenience of this tutorial.
    
            ArcGISRuntimeEnvironment.setApiKey("YOUR_API_KEY")
    
        }
    
        // 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
    82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 83 84 85 86 87 88 89 90 90 90 90 90 90 90 90 90 89 88 87 86 85 85 85 86 86 85 84 83 82 81 80 79 79
    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
    113
    114
    115
    /*
     * Copyright 2020 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.
     */
    
    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
    
    
    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)
    
            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))
    
        }
    
        override fun onPause() {
            mapView.pause()
            super.onPause()
        }
    
        override fun onResume() {
            super.onResume()
            mapView.resume()
        }
    
        override fun onDestroy() {
            mapView.dispose()
            super.onDestroy()
        }
    
        private fun setApiKeyForApp(){
            // set your API key
            // Note: it is not best practice to store API keys in source code. The API key is referenced
            // here for the convenience of this tutorial.
    
            ArcGISRuntimeEnvironment.setApiKey("YOUR_API_KEY")
    
        }
    
        // 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]
                }
    
                else {
                    // log an error if the mobile map package fails to load
                    logError(mapPackage.loadError.message)
                }
    
            }
    
        } // end of loadMobileMapPackage()
    
        private fun logError(message: String?) {
            message?.let {
                Log.e(TAG, it)
                Toast.makeText(this, it, Toast.LENGTH_LONG).show()
            }
        }
    
    }
    
  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
    82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 98 98 98 98 98 99 100 101 101 100 99 98 97 96 95 94 94
    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
    113
    114
    115
    /*
     * Copyright 2020 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.
     */
    
    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
    
    
    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)
    
            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))
    
        }
    
        override fun onPause() {
            mapView.pause()
            super.onPause()
        }
    
        override fun onResume() {
            super.onResume()
            mapView.resume()
        }
    
        override fun onDestroy() {
            mapView.dispose()
            super.onDestroy()
        }
    
        private fun setApiKeyForApp(){
            // set your API key
            // Note: it is not best practice to store API keys in source code. The API key is referenced
            // here for the convenience of this tutorial.
    
            ArcGISRuntimeEnvironment.setApiKey("YOUR_API_KEY")
    
        }
    
        // 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]
                }
    
                else {
                    // log an error if the mobile map package fails to load
                    logError(mapPackage.loadError.message)
                }
    
            }
    
        } // end of loadMobileMapPackage()
    
        private fun logError(message: String?) {
            message?.let {
                Log.e(TAG, it)
                Toast.makeText(this, it, Toast.LENGTH_LONG).show()
            }
        }
    
    }
    
  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
    90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 89 88 88 88 88 88 88 88 88 88 88 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 102 102 102 101 100 99 98 97 96 95 95
    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
    113
    114
    115
    /*
     * Copyright 2020 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.
     */
    
    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
    
    
    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)
    
            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))
    
        }
    
        override fun onPause() {
            mapView.pause()
            super.onPause()
        }
    
        override fun onResume() {
            super.onResume()
            mapView.resume()
        }
    
        override fun onDestroy() {
            mapView.dispose()
            super.onDestroy()
        }
    
        private fun setApiKeyForApp(){
            // set your API key
            // Note: it is not best practice to store API keys in source code. The API key is referenced
            // here for the convenience of this tutorial.
    
            ArcGISRuntimeEnvironment.setApiKey("YOUR_API_KEY")
    
        }
    
        // 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]
                }
    
                else {
                    // log an error if the mobile map package fails to load
                    logError(mapPackage.loadError.message)
                }
    
            }
    
        } // end of loadMobileMapPackage()
    
        private fun logError(message: String?) {
            message?.let {
                Log.e(TAG, it)
                Toast.makeText(this, it, Toast.LENGTH_LONG).show()
            }
        }
    
    }
    
  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
    105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 106 107 108 109 110 111 112 113 113 113
    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
    113
    114
    115
    /*
     * Copyright 2020 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.
     */
    
    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
    
    
    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)
    
            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))
    
        }
    
        override fun onPause() {
            mapView.pause()
            super.onPause()
        }
    
        override fun onResume() {
            super.onResume()
            mapView.resume()
        }
    
        override fun onDestroy() {
            mapView.dispose()
            super.onDestroy()
        }
    
        private fun setApiKeyForApp(){
            // set your API key
            // Note: it is not best practice to store API keys in source code. The API key is referenced
            // here for the convenience of this tutorial.
    
            ArcGISRuntimeEnvironment.setApiKey("YOUR_API_KEY")
    
        }
    
        // 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]
                }
    
                else {
                    // log an error if the mobile map package fails to load
                    logError(mapPackage.loadError.message)
                }
    
            }
    
        } // end of loadMobileMapPackage()
    
        private fun logError(message: String?) {
            message?.let {
                Log.e(TAG, it)
                Toast.makeText(this, it, Toast.LENGTH_LONG).show()
            }
        }
    
    }
    

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.

    The Android Emulator should display and run your app in the Android Virtual Devcie (AVD) selected in the Android Studio toolbar:

    Currently selected AVD in Android Studio

    If your app builds but no AVD displays, you need to add one. Click Tools > AVD Manager > Create Virtual Device...

    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.

    If you already closed the AVD on which you installed your app, then launch the Android Emulator manually.

    1. Click Tools > AVD Manager. In the Android Virtual Device Manager, double-click on the AVD you used before.
  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 platform 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.