Use the OfflineMapTask
to take a web map offline, but instead of downloading an online basemap, use one which is already on the device.
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
- Create a
PortalItem
object using a web map's ID and create anArcGISMap
from it. - Initialize an
OfflineMapTask
object using the map. - Request the default parameters for the task with
OfflineMapTask.createDefaultGenerateOfflineMapParametersAsync()
. - A
GenerateOfflineMapJob
is created by callingOfflineMapTask.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.
- If desired, set the
- Run the
GenerateOfflineMapJob
with basemap settings from step 4.
Relevant API
- GenerateOfflineMapJob
- GenerateOfflineMapParameters
- GenerateOfflineMapResult
- OfflineMapTask
Offline Data
- Download the data from ArcGIS Online.
- Open your command prompt and navigate to the folder where you extracted the contents of the data from step 1.
- 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
/*
* 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();
}
}