Overview

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

The ArcGIS Runtime SDK for Java 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.

Before you begin

Reuse the starter project

If you have not done the starter project lab, be sure to review it first so you are familiar with running the app.

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

Steps

Update the project

  1. Open the settings.gradle file in an editor of your choice and change the rootProject.name value to add-layers-to-a-map-offline.

Load your map package on to your device

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

Imports

  1. Open the file src/main/java/com/arcgis/developers/labs/App.java. Add these new import statements:
    import com.esri.arcgisruntime.mapping.MobileMapPackage;
    import com.esri.arcgisruntime.loadable.LoadStatus;
    

Load and display the map package

  1. Add a new private method setupMobileMap that loads a mmpk file into a map object and then updates the map view to display it. The map package is loaded asynchronously so it does not slow the UI. If the map package fails to load for any reason, we fallback to the online map we had before.

      private void setupMobileMap() {
        if (mapView != null) {
          String mmpkFile = "./devlabs-package.mmpk";
          final MobileMapPackage mapPackage = new MobileMapPackage(mmpkFile);
          mapPackage.addDoneLoadingListener(() -> {
            if (mapPackage.getLoadStatus() == LoadStatus.LOADED && mapPackage.getMaps().size() > 0) {
              mapView.setMap(mapPackage.getMaps().get(0));
            } else {
              setupMap();
            }
          });
          mapPackage.loadAsync();
        }
      }
    
  2. Look for these lines of the code in the start method

    mapView.setMap(map);
    setupMap();
    

    and replace setupMap() with setupMobileMap():

    mapView.setMap(map);
    setupMobileMap();
    

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.

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.