Display a map from a mobile map package

Learn how to display a map from a mobile map package (.mmpk file) when you're offline.

display a map from a mobile map package

In this tutorial you use the MobileMapPackage class to access the .mmpk file, MahouRivieraTrails.mmpk, and load it to read its contents. The map within the mobile map package 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. Confirm that your system meets the minimum system requirements.
  3. An IDE for Java.

Steps

Open a Java project

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

  2. Open the build.gradle file as a project in IntelliJ IDEA.

  3. If you downloaded the solution project, set your API key.

    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 the IntelliJ IDEA > Project tool window, open src/main/java/com.example.app and click App.

    3. In the start() method, set the API key property on the ArcGISRuntimeEnvironment with your API key. Replace YOUR_API_KEY with your actual API Key. Be sure to surround your API Key with quotes, because the parameter passed to setApiKey is a string.

      App.java
      Use dark colors for code blocks
                                                                                   
      Change 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
      
      //   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
      //
      //   http://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 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 javafx.application.Application;
      import javafx.scene.Scene;
      import javafx.scene.layout.StackPane;
      import javafx.stage.Stage;
      
      public class App extends Application {
      
        private MapView mapView;
      
        public static void main(String[] args) {
          Application.launch(args);
        }
      
        @Override
        public void start(Stage stage) {
          // set the title and size of the stage and show it
          stage.setTitle("Display a map tutorial");
          stage.setWidth(800);
          stage.setHeight(700);
          stage.show();
      
          // create a JavaFX scene with a stack pane as the root node, and add it to the scene
          StackPane stackPane = new StackPane();
          Scene scene = new Scene(stackPane);
          stage.setScene(scene);
      
          // 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.
          String yourApiKey = "YOUR_API_KEY";
          ArcGISRuntimeEnvironment.setApiKey(yourApiKey);
      
          // create a map view to display the map and add it to the stack pane
          mapView = new MapView();
          stackPane.getChildren().add(mapView);
      
          ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC);
      
          // set the map on the map view
          mapView.setMap(map);
      
          mapView.setViewpoint(new Viewpoint(34.02700, -118.80543, 144447.638572));
      
        }
      
        /**
         * Stops and releases all resources used in application.
         */
        @Override
        public void stop() {
          if (mapView != null) {
            mapView.dispose();
          }
        }
      
      }
      

Add import statements

In App.Java, add the following import statements.

App.java
Use dark colors for code blocks
                                                                                             
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

//   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
//
//   http://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 java.io.File;
import javafx.scene.control.Alert;
import com.esri.arcgisruntime.mapping.MobileMapPackage;

import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
import com.esri.arcgisruntime.mapping.view.MapView;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import com.esri.arcgisruntime.loadable.LoadStatus;


public class App extends Application {
    private MapView mapView;

    private MobileMapPackage mobileMapPackage;

    @Override
    public void start(Stage stage) {

        try {

            // create the stack pane and JavaFX application scene
            StackPane stackPane = new StackPane();
            Scene scene = new Scene(stackPane);
            // set title, size, and add scene to stage
            stage.setTitle("Display a map from a mobile map package");
            stage.setWidth(800);
            stage.setHeight(700);
            stage.setScene(scene);
            stage.show();

            // 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.
            String yourApiKey = "YOUR_API_KEY";
            ArcGISRuntimeEnvironment.setApiKey(yourApiKey);

            // create a map view
            mapView = new MapView();

            //Set the location of the .mmpk file to the project root folder
           final String mmpkPath = new File(System.getProperty("user.dir"), "./MahouRivieraTrails.mmpk").getAbsolutePath();
            mobileMapPackage = new MobileMapPackage(mmpkPath);

            mobileMapPackage.loadAsync();
            mobileMapPackage.addDoneLoadingListener(() -> {
                if (mobileMapPackage.getLoadStatus() == LoadStatus.LOADED && mobileMapPackage.getMaps().size() > 0) {
                    //add the map from the mobile map package to the map view
                    mapView.setMap(mobileMapPackage.getMaps().get(0));
                } else {
                    Alert alert = new Alert(Alert.AlertType.ERROR, "Failed to load the mobile map package");
                    alert.show();
                }
            });

            // add the map view to stack pane
            stackPane.getChildren().add(mapView);

        } catch (Exception e) {
            // on any error, display the stack trace
            e.printStackTrace();
        }

    }
   // Stops and releases all resources used in application.
       @Override
    public void stop() {
        if (mapView != null) {
            mapView.dispose();
        }
    }
    public static void main (String[] args){
        Application.launch(args);
    }
}

Specify a location for the .mmpk file

  1. Make note of where the mobile map package (MahouRivieraTrails.mmpk) is. It's at the top level of the solution referenced in the Open a Java project step above.

  2. Copy the .mmpk file onto a location you choose, then point your app to it as you would any file. For example, you can use Java's System.setProperty() to specify a new system property whose value is the directory where you placed the .mmpk file, and then call System.getProperty() with that property name to get the path to the file. The following code shows how to use the existing system property user.dir key, which points to the project's root directory.

Or, you can use the system property user.home and copy the .mmpk file to your home directory (for Windows, this is C:\Users\<username>).

After getting the path to your .mmpk file, create a MobileMapPackage and assign it to the mobileMapPackage variable. Also surround your code with a Java try statement, which will be later followed by a catch that prints a standard stack trace.

App.java
Use dark colors for code blocks
                                                                                             
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

//   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
//
//   http://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 java.io.File;
import javafx.scene.control.Alert;
import com.esri.arcgisruntime.mapping.MobileMapPackage;

import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
import com.esri.arcgisruntime.mapping.view.MapView;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import com.esri.arcgisruntime.loadable.LoadStatus;


public class App extends Application {
    private MapView mapView;

    private MobileMapPackage mobileMapPackage;

    @Override
    public void start(Stage stage) {

        try {

            // create the stack pane and JavaFX application scene
            StackPane stackPane = new StackPane();
            Scene scene = new Scene(stackPane);
            // set title, size, and add scene to stage
            stage.setTitle("Display a map from a mobile map package");
            stage.setWidth(800);
            stage.setHeight(700);
            stage.setScene(scene);
            stage.show();

            // 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.
            String yourApiKey = "YOUR_API_KEY";
            ArcGISRuntimeEnvironment.setApiKey(yourApiKey);

            // create a map view
            mapView = new MapView();

            //Set the location of the .mmpk file to the project root folder
           final String mmpkPath = new File(System.getProperty("user.dir"), "./MahouRivieraTrails.mmpk").getAbsolutePath();
            mobileMapPackage = new MobileMapPackage(mmpkPath);

            mobileMapPackage.loadAsync();
            mobileMapPackage.addDoneLoadingListener(() -> {
                if (mobileMapPackage.getLoadStatus() == LoadStatus.LOADED && mobileMapPackage.getMaps().size() > 0) {
                    //add the map from the mobile map package to the map view
                    mapView.setMap(mobileMapPackage.getMaps().get(0));
                } else {
                    Alert alert = new Alert(Alert.AlertType.ERROR, "Failed to load the mobile map package");
                    alert.show();
                }
            });

            // add the map view to stack pane
            stackPane.getChildren().add(mapView);

        } catch (Exception e) {
            // on any error, display the stack trace
            e.printStackTrace();
        }

    }
   // Stops and releases all resources used in application.
       @Override
    public void stop() {
        if (mapView != null) {
            mapView.dispose();
        }
    }
    public static void main (String[] args){
        Application.launch(args);
    }
}

Load the .mmpk file and display its map

  1. The following code shows how to load the .mmpk file to get a list of its maps and then select the map you want to display. It also adds the try statement's closing catch statement.

    App.java
    Use dark colors for code blocks
                                                                                                 
    Add line.Add line.Add line.Add line.Add line.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
    
    //   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
    //
    //   http://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 java.io.File;
    import javafx.scene.control.Alert;
    import com.esri.arcgisruntime.mapping.MobileMapPackage;
    
    import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
    import com.esri.arcgisruntime.mapping.view.MapView;
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    import com.esri.arcgisruntime.loadable.LoadStatus;
    
    
    public class App extends Application {
        private MapView mapView;
    
        private MobileMapPackage mobileMapPackage;
    
        @Override
        public void start(Stage stage) {
    
            try {
    
                // create the stack pane and JavaFX application scene
                StackPane stackPane = new StackPane();
                Scene scene = new Scene(stackPane);
                // set title, size, and add scene to stage
                stage.setTitle("Display a map from a mobile map package");
                stage.setWidth(800);
                stage.setHeight(700);
                stage.setScene(scene);
                stage.show();
    
                // 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.
                String yourApiKey = "YOUR_API_KEY";
                ArcGISRuntimeEnvironment.setApiKey(yourApiKey);
    
                // create a map view
                mapView = new MapView();
    
                //Set the location of the .mmpk file to the project root folder
               final String mmpkPath = new File(System.getProperty("user.dir"), "./MahouRivieraTrails.mmpk").getAbsolutePath();
                mobileMapPackage = new MobileMapPackage(mmpkPath);
    
                mobileMapPackage.loadAsync();
                mobileMapPackage.addDoneLoadingListener(() -> {
                    if (mobileMapPackage.getLoadStatus() == LoadStatus.LOADED && mobileMapPackage.getMaps().size() > 0) {
                        //add the map from the mobile map package to the map view
                        mapView.setMap(mobileMapPackage.getMaps().get(0));
                    } else {
                        Alert alert = new Alert(Alert.AlertType.ERROR, "Failed to load the mobile map package");
                        alert.show();
                    }
                });
    
                // add the map view to stack pane
                stackPane.getChildren().add(mapView);
    
            } catch (Exception e) {
                // on any error, display the stack trace
                e.printStackTrace();
            }
    
        }
       // Stops and releases all resources used in application.
           @Override
        public void stop() {
            if (mapView != null) {
                mapView.dispose();
            }
        }
        public static void main (String[] args){
            Application.launch(args);
        }
    }
    
  2. Run the app.

    Ensure to run the app as a Gradle task and not as an application in your IDE. In the Gradle tool window, under Tasks > application, click run.

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

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