Take a web map offline with additional options for each layer.
Use case
When taking a web map offline, you may adjust the data (such as layers or tiles) that is downloaded by using custom parameter overrides. This can be used to reduce the extent of the map or the download size of the offline map. It can also be used to highlight specific data by removing irrelevant data. Additionally, this workflow allows you to take features offline that don't have a geometry - for example, features whose attributes have been populated in the office, but still need a site survey for their geometry.
How to use the sample
Modify the overrides parameters:
- Use the sliders to adjust the the minimum and maximum scale levels and buffer radius to be taken offline for the streets basemap.
- Toggle the switches for the feature operational layers you want to include in the offline map.
- Use the min hydrant flow rate slider to only download features with a flow rate higher than this value.
- Turn on the "Water Pipes" switch if you want to crop the water pipe features to the extent of the map.
After you have set up the overrides to your liking, tap "Start" to start the download. A progress bar will display. Tap "Cancel" if you want to stop the download. When the download is complete, the view will display the offline map. Pan around to see that it is cropped to the download area's extent.
How it works
- Load a web map from a
PortalItem
. Authenticate with the portal if required. - Create an
OfflineMapTask
with the map. - Generate default task parameters using the extent area you want to download with the
OfflineMapTask.makeDefaultGenerateOfflineMapParameters(withAreaOfInterest:)
method. - Generate additional "override" parameters using the default parameters with the
OfflineMapTask.makeGenerateOfflineMapParameterOverrides(parameters:)
method. - For the basemap:
- Get the parameters
OfflineMapParametersKey
for the basemap layer. - Get the
ExportTileCacheParameters
for the basemap layer fromGenerateOfflineMapParameterOverrides exportTileCacheParameters[key]
with the key above. - Set the level IDs you want to download by setting the
levelIDs
property ofExportTileCacheParameters
. - To buffer the extent, set a buffered geometry to the
areaOfInterest
property ofExportTileCacheParameters
, where the buffered geometry can be calculated with theGeometryEngine
.
- Get the parameters
- To remove operational layers from the download:
- Create an
OfflineMapParametersKey
with the operational layer. - Use the key to obtain the relevant
GenerateGeodatabaseParameters
from thegenerateGeodatabaseParameters
property ofGenerateOfflineMapParameterOverrides
. - Remove the
GenerateLayerOption
from thelayerOptions
where the option's ID matches theserviceLayerID
.
- Create an
- To filter the features downloaded in an operational layer:
- Get the layer options for the operational layer using the directions in step 6.
- Loop through the layer options. If the option layerID matches the layer's ID, set the filter's
whereClause
property.
- To not crop a layer's features to the extent of the offline map (default is true):
- Set
useGeometry
property ofGenerateLayerOption
to false.
- Set
- Create a
GenerateOfflineMapJob
withOfflineMapTask.makeGenerateOfflineMapJob(parameters:downloadDirectory:overrides:)
. Start the job withGenerateOfflineMapJob.start()
. - When
GenerateOfflineMapJog.output
is done, get a reference to the offline map withoutput.offlineMap
.
Relevant API
- ExportTileCacheParameters
- GenerateGeodatabaseParameters
- GenerateLayerOption
- GenerateOfflineMapJob
- GenerateOfflineMapParameterOverrides
- GenerateOfflineMapParameters
- GenerateOfflineMapResult
- OfflineMapParametersKey
- OfflineMapTask
Additional information
For applications where you just need to take all layers offline, use the standard workflow (using only GenerateOfflineMapParameters
). For a simple example of how you take a map offline, please consult the "Generate offline map" sample.
Tags
adjust, download, extent, filter, LOD, offline, override, parameters, reduce, scale range, setting
Sample Code
// Copyright 2024 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
//
// https://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.
import ArcGIS
import SwiftUI
struct GenerateOfflineMapWithCustomParametersView: View {
/// A Boolean value indicating whether the job is generating an offline map.
@State private var isGeneratingOfflineMap = false
/// A Boolean value indicating whether the job is cancelling.
@State private var isCancellingJob = false
/// The error shown in the error alert.
@State private var error: Error?
/// The view model for this sample.
@StateObject private var model = Model()
/// A Boolean value indicating whether to set the custom parameters.
@State private var isShowingSetCustomParameters = false
var body: some View {
GeometryReader { geometry in
MapViewReader { mapView in
MapView(map: model.offlineMap ?? model.onlineMap)
.interactionModes(isGeneratingOfflineMap ? [] : [.pan, .zoom])
.errorAlert(presentingError: $error)
.task {
do {
try await model.initializeOfflineMapTask()
} catch {
self.error = error
}
}
.onDisappear {
Task { await model.cancelJob() }
}
.overlay {
Rectangle()
.stroke(.red, lineWidth: 2)
.padding(EdgeInsets(top: 20, leading: 20, bottom: 44, trailing: 20))
.opacity(model.offlineMap == nil ? 1 : 0)
}
.overlay {
if isGeneratingOfflineMap,
let progress = model.generateOfflineMapJob?.progress {
VStack(spacing: 16) {
ProgressView(progress)
.progressViewStyle(.linear)
.frame(maxWidth: 200)
Button("Cancel") {
isCancellingJob = true
}
.disabled(isCancellingJob)
.task(id: isCancellingJob) {
guard isCancellingJob else { return }
// Cancels the job.
await model.cancelJob()
// Sets cancelling the job and generating an
// offline map to false.
isCancellingJob = false
isGeneratingOfflineMap = false
}
}
.padding()
.background(.regularMaterial)
.clipShape(RoundedRectangle(cornerRadius: 15))
.shadow(radius: 3)
}
}
.overlay(alignment: .top) {
Text("Offline map generated.")
.frame(maxWidth: .infinity, alignment: .center)
.padding(8)
.background(.thinMaterial, ignoresSafeAreaEdges: .horizontal)
.opacity(model.offlineMap != nil ? 1 : 0)
}
.toolbar {
ToolbarItem(placement: .bottomBar) {
Button("Generate Offline Map") {
// Creates a rectangle from the area of interest.
let viewRect = geometry.frame(in: .local).inset(
by: UIEdgeInsets(
top: 20,
left: geometry.safeAreaInsets.leading + 20,
bottom: 44,
right: -geometry.safeAreaInsets.trailing + 20
)
)
// Creates an envelope from the rectangle.
model.extent = mapView.envelope(fromViewRect: viewRect)
isShowingSetCustomParameters.toggle()
}
.disabled(model.isGenerateDisabled || isGeneratingOfflineMap)
.popover(isPresented: $isShowingSetCustomParameters) {
CustomParameters(
model: model,
isGeneratingOfflineMap: $isGeneratingOfflineMap
)
.frame(idealWidth: 350, idealHeight: 750)
}
.task(id: isGeneratingOfflineMap) {
guard isGeneratingOfflineMap else { return }
do {
// Generates an offline map.
try await model.generateOfflineMap()
} catch {
self.error = error
}
// Sets generating an offline map to false.
isGeneratingOfflineMap = false
}
}
}
}
}
}
}
#Preview {
NavigationStack {
GenerateOfflineMapWithCustomParametersView()
}
}