Generate offline map with local basemap

View on GitHubSample viewer app

Use the OfflineMapTask to take a web map offline, but instead of downloading an online basemap, use one which is already on the device.

Image of generate offline map with local basemap

Use case

There are a number of use-cases where you may wish to use a basemap which is already on the device, rather than downloading:

  • You want to limit the total download size.
  • You want to be able to share a single set of basemap files between many offline maps.
  • You want to use a custom basemap (for example authored in ArcGIS Pro) which is not available online.
  • You do not wish to sign into ArcGIS.com in order to download Esri basemaps.

The author of a web map can support the use of basemaps which are already on a device by configuring the web map to specify the name of a suitable basemap file. This could be a basemap which:

  • Has been authored in ArcGIS Pro to make use of your organizations custom data.
  • Is available as a PortalItem which can be downloaded once and re-used many times.

How to use the sample

Tap on "Generate Offline Map". You will be prompted to choose whether you wish to download the online basemap or use the "naperville_imagery.tpkx" basemap (see Offline Data section). If you choose to download the online basemap, the offline map will be generated with the same (topographic) basemap as the online web map. To download the Esri basemap, an organizational account is required.

If you choose to use the basemap from the device, the offline map will be generated with the local imagery basemap. The download will be quicker since no tiles are exported or downloaded. Since the application is not exporting online ArcGIS Online basemaps you will not need to log-in.

How it works

  1. Create a PortalItem object using a web map's ID and create an ArcGISMap from it.
  2. Initialize an OfflineMapTask object using the map.
  3. Request the default parameters for the task with OfflineMapTask.createDefaultGenerateOfflineMapParametersAsync().
  4. A GenerateOfflineMapJob is created by calling OfflineMapTask.generateOfflineMap.
    • If desired, set the GenerateOfflineMapParameters.referenceBasemapDirectory to the absolute path of the directory which contains the .tpkx file.
    • Otherwise a basemap will be downloaded.
  5. Run the GenerateOfflineMapJob with basemap settings from step 4.

Relevant API

  • GenerateOfflineMapJob
  • GenerateOfflineMapParameters
  • GenerateOfflineMapResult
  • OfflineMapTask

Offline Data

  1. Download the data from ArcGIS Online.
  2. Open your command prompt and navigate to the folder where you extracted the contents of the data from step 1.
  3. Push the data into the scoped storage of the sample app:

adb push naperville_imagery.tpk /Android/data/com.esri.arcgisruntime.sample.generateofflinemapwithlocalbasemap/files/naperville_imagery.tpkx

Tags

basemap, download, local, offline, save, web map

Sample Code

LocalBasemapAlertDialogFragment.javaLocalBasemapAlertDialogFragment.javaMainActivity.javaProgressDialogFragment.java
Use dark colors for code blocksCopy
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 * Copyright 2019 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.esri.arcgisruntime.sample.generateofflinemapwithlocalbasemap;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

public class LocalBasemapAlertDialogFragment extends DialogFragment {

  private static final String ARGS_TITLE = ProgressDialogFragment.class.getSimpleName() + "_title";
  private static final String ARGS_MESSAGE = ProgressDialogFragment.class.getSimpleName() + "_message";
  private static final String ARGS_POSITIVE = ProgressDialogFragment.class.getSimpleName() + "_positive";
  private static final String ARGS_NEGATIVE = ProgressDialogFragment.class.getSimpleName() + "_negative";

  private LocalBasemapAlertDialogFragment.OnClickListener mOnClickListener;

  private String mTitle;
  private String mMessage;
  private String mPositive;
  private String mNegative;

  public static LocalBasemapAlertDialogFragment newInstance(String title, String message, String positive, String negative) {
    LocalBasemapAlertDialogFragment fragment = new LocalBasemapAlertDialogFragment();
    Bundle args = new Bundle();
    args.putString(ARGS_TITLE, title);
    args.putString(ARGS_MESSAGE, message);
    args.putString(ARGS_POSITIVE, positive);
    args.putString(ARGS_NEGATIVE, negative);
    fragment.setArguments(args);
    return fragment;
  }

  @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // prevent re-creation during configuration chance to allow us to dismiss this DialogFragment
    setRetainInstance(true);
    setCancelable(false);

    if (getArguments() != null) {
      mTitle = getArguments().getString(ARGS_TITLE);
      mMessage = getArguments().getString(ARGS_MESSAGE);
      mPositive = getArguments().getString(ARGS_POSITIVE);
      mNegative = getArguments().getString(ARGS_NEGATIVE);
    }
  }

  @Override public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof LocalBasemapAlertDialogFragment.OnClickListener) {
      mOnClickListener = (LocalBasemapAlertDialogFragment.OnClickListener) context;
    }
  }

  @NonNull @Override public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
    super.onCreateDialog(savedInstanceState);
    // create a dialog to show progress
    return new AlertDialog.Builder(getActivity())
        .setTitle(mTitle)
        .setMessage(mMessage)
        .setPositiveButton(mPositive, (dialog, which) -> {
          if (mOnClickListener != null) {
            mOnClickListener.onPositiveClick();
          }
        })
        .setNegativeButton(mNegative, (dialog, which) -> {
          if (mOnClickListener != null) {
            mOnClickListener.onNegativeClick();
          }
        })
        .create();
  }

  @Override
  public void onDestroyView() {
    Dialog dialog = getDialog();
    // handles https://code.google.com/p/android/issues/detail?id=17423
    if (dialog != null && getRetainInstance()) {
      dialog.setDismissMessage(null);
    }
    super.onDestroyView();
  }

  interface OnClickListener {
    void onPositiveClick();
    void onNegativeClick();
  }

}

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