Overview

You will learn: how to load a basemap and feature layers from a mobile map package (.mmpk) file.

The ArcGIS Runtime SDK for Android allows you to provide offline maps to your users so they can be productive when their network connectivity is poor or offline. The map package is a file created with another tool, such as ArcGIS Pro, and loaded to your user's device.

In this lab, you will use the ArcGIS Runtime SDK for Android to open a mobile map package on a device when offline. The package is created with ArcGIS Pro, ArcGIS Online or ArcGIS Enterprise using the ArcGIS REST API and loaded to your device as part of application deployment.

Before you begin

Make sure you have installed the latest version of Android Studio.

Reuse the starter project

In a new or empty project folder, make a copy of the Create a starter app or download and unzip the project solution.

  • Open the new Android project in Android Studio.

  • Run the project and verify the project builds and the map displays in the device simulator.

  • You may be required to sync the project dependencies.

Prepare your data

Complete the lab Prepare your data for offline use. You will use the map package file created in that lab for the offline map data required in this lab.

Steps

Load your map package on to your device

  1. You created a map package in the lab Prepare your data for offline use. Load the map package file created in that lab on to your device. If you did not complete that lab, download the map package we prepared.

Load and display the map package

  1. In the file app > java > {your.package.name} > MainActivity.java, add a new method named setupOfflineMap to load the mmpk file into an ArcGISMap object and then update your MapView to display it.

     private void setupOfflineMap() {
         if (mMapView != null) {
             File path = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
             File mmpkFile = new File(path, "devlabs-package.mmpk");
             final MobileMapPackage mapPackage = new MobileMapPackage(mmpkFile.getAbsolutePath());
             mapPackage.addDoneLoadingListener(() -> {
                 if (mapPackage.getLoadStatus() == LoadStatus.LOADED && !mapPackage.getMaps().isEmpty()) {
                     mMapView.setMap(mapPackage.getMaps().get(0));
                 } else {
                     Log.e("setupOfflineMap", "Cannot load " + path.toString() + "/devlabs-package.mmpk");
                     setupMap();
                 }
             });
             mapPackage.loadAsync();
         }
     }
    
  2. In the same file, find the onCreate() method. Replace the call to setupMap() with a call your new method setupOfflineMap().

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         mMapView = findViewById(R.id.mapView);
         // *** ADD ***
         setupOfflineMap();
    
  3. Run your app and test the code in the Android emulator to see the map.

Congratulations, you're done!

Your map, as it was designed in ArcGIS Pro, should load and display with the view point centered on the Los Angeles area. Compare your solution with our completed solution project.

Challenge

Change map contents

Learn how you can change many elements of your map without writing any code. Return to ArcGIS Pro and alter your map: change the extent, add or remove layers, change symbology. Republish your map package and load it on your device.