Learn how to display a map from a mobile map package (MMPK).
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:
An ArcGIS account to access API keys. If you don't have an account, sign up for free.
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
To start this tutorial, complete the Display a map tutorial. Or download and unzip the Display a map solution in a new folder.
Modify the old project for use in this new tutorial. Expand More info for instructions.
On your file system, delete the .idea folder, if present, at the top level of your project.
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
Use dark colors for code blocks
Change line
1
2
3
4
5
6
7
<resources><stringname="app_name">Display a map from a mobile map package</string></resources>
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
Expand
Use dark colors for code blocks
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
rootProject.name = "Display a map from a mobile map package"include':app'
Click File > Sync Project with Gradle files. Android Studio will recognize your changes and create a new .idea folder.
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.
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.
In Android Studio: in the Android tool window, open app > java > com.example.app > MainActivity.
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.
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.
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><stringname="app_name">Display a map from a mobile map package</string><stringname="mahourivieratrails_mmpk">/MahouRivieraTrails.mmpk</string></resources>
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
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
// set up your map here. You will call this method from onCreate()privatefunsetupMap() {
// create a map with the BasemapStyle streetsval 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))
}
Create a loadlMobileMapPackage() function that takes the .mmpk's path and file name. In the function, create the MobileMapPackage and load the package.
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.
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
// add done listener which will invoke when mobile map package has loaded mapPackage.addDoneLoadingListener() {
// check load status and that the mobile map package has mapsif (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
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.
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.
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:
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.
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.
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.
Click Tools > AVD Manager. In the Android Virtual Device Manager, double-click on the AVD you used before.
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.
Upload the MahouRivieraTrails.mmpk file from your development computer.
In Device File Manager, right-click on the sdcard > Android > data directory and click Synchronize.
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
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.
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.