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.

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 on the device simulator.

  • You may be required to sync the project dependencies.

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 that file 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 to load the mmpk file into an ArcGISMap object and then update your MapView to display it.

     private void setupMobileMap() {
         if (mMapView != null) {
             File mmpkFile = new File(Environment.getExternalStorageDirectory(), "devlabs-package.mmpk");
             final MobileMapPackage mapPackage = new MobileMapPackage(mmpkFile.getAbsolutePath());
             mapPackage.addDoneLoadingListener(new Runnable() {
                 @Override
                 public void run() {
                     // Verify the file loaded and there is at least one map
                     if (mapPackage.getLoadStatus() == LoadStatus.LOADED && mapPackage.getMaps().size() > 0) {
                         mMapView.setMap(mapPackage.getMaps().get(0));
                     } else {
                         // Error if the mobile map package fails to load or there are no maps included in the package
                         setupMap();
                     }
                 }
             });
             mapPackage.loadAsync();
         }
     }
    
  2. In the same file, find the onCreate method, then call your new method after creating the MapView.

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         mMapView = (MapView) findViewById(R.id.mapView);
         // *** ADD ***
         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.