Introduction to ArcGIS Runtime SDK for Android (19): Display Local Map Data - mmpk Mobile Map Pack

Keywords: Android Mobile SDK xml

This article mainly explains how to display the local mmpk mobile map package files with the help of ArcGIS Runtime SDK for Android.

Implementation steps:

1. Create an Android project

2. Adding Runtime SDK dependencies

The first two steps are omitted. Beginners can refer to them. Introduction to ArcGIS Runtime SDK for Android (1): First Map Application (2-D) 

3. Adding permissions and OpenGL ES support

Add in Android Manifest. xml:

    <! - Internet access - >
    <uses-permission android:name="android.permission.INTERNET"/>
    <! - Read external storage permissions - >
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

4. Setting up the interface layout

Layout XML code:

    <!-- MapView control -->
    <com.esri.arcgisruntime.mapping.view.MapView
            android:id="@+id/mapView"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    </com.esri.arcgisruntime.mapping.view.MapView>

5. Write code:

Train of thought:

(1) Request file read and write permission (Android version 6.0 and above)

(2) Get the mmpk file path

(3) Create the Mobile Map Package mobile map package object through the path, and load it asynchronously.

(4) Obtain all ArcGIS Map map objects in Mobile Map Package Mobile Map Package and select one for display.

(5) ArcGIS Map map is displayed in MapView control.

Prepare: Create ArcGIS folder in the built-in SD card of Android device and put it in Yellowstone.mmpk

Steps:

(1) Variable preparation:

    //MapView control variable
    private MapView mainMapView;
    //Mobile Map Packet Variables
    private MobileMapPackage mainMobileMapPackage;

(2) onCreate method:

     //Request device read and write permission and load data
     requestWritePermission();

(3) Methodological support:

    //Request device read and write permission and load data
    private void requestWritePermission() {
        // Define request permissions
        String[] reqPermission = new String[] { Manifest.permission.READ_EXTERNAL_STORAGE };
        int requestCode = 2;
        // In API version 23 or above, permissions need to be requested at runtime
        if (ContextCompat.checkSelfPermission(MainActivity.this,
                reqPermission[0]) == PackageManager.PERMISSION_GRANTED) {
            //Loading data
            loadmmpk();
        } else {
            // Request permission
            ActivityCompat.requestPermissions(MainActivity.this, reqPermission, requestCode);
        }
    }
   
    //Processing privilege request response, user response after choosing privilege
    public void onRequestPermissionsResult(int requestCode,
                                           @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            //Loading data
            loadmmpk();
        } else {
            Toast.makeText(MainActivity.this, "Read and write permission denied", Toast.LENGTH_SHORT).show();
        }
    }
   
    //Loading mmpk files
    private void loadmmpk() {
        try {
            String mainMMPKPath = getSDCardPath()+"/ArcGIS/Yellowstone.mmpk";
            mainMapView = (MapView) findViewById(R.id.mainMapView);
            mainMobileMapPackage = new MobileMapPackage(mainMMPKPath);
            //Asynchronous loading of mobile map packages
            mainMobileMapPackage.loadAsync();
            mainMobileMapPackage.addDoneLoadingListener(new Runnable() {
                @Override
                public void run() {
                    LoadStatus mainLoadStatus = mainMobileMapPackage.getLoadStatus();
                    if (mainLoadStatus == LoadStatus.LOADED) {
                        //All maps in the Mobile Map Pack
                        List<ArcGISMap> mainArcGISMapL = mainMobileMapPackage.getMaps();
                        //Get a map with index 0
                        ArcGISMap mainArcGISMap = mainArcGISMapL.get(0);
                        //Get the base map in mmpk (alternate)
                        Basemap mainBasemap = mainArcGISMap.getBasemap();
                        //Get the business layer in mmpk (alternate)
                        LayerList mainMMPKLL = mainArcGISMap.getOperationalLayers();
                        //Set up the map to display in MapView
                        mainMapView.setMap(mainArcGISMap);
                    }
                }
            });
        } catch (Exception e) {
        }
    }

    //Getting SD Card Path
    public String getSDCardPath()
    {
        return Environment.getExternalStorageDirectory().getAbsolutePath()
                + File.separator;
    }

6. Running APP: Simple browsing of maps in mobile map packages

Thank you, Mr. luq, for your guidance.

Posted by Wes1890 on Tue, 05 Feb 2019 00:03:16 -0800