Generate offline map with custom parameters

View on GitHub

Take a web map offline with additional options for each layer.

Image of Generate offline map with custom parameters sample

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

  1. Load a web map from a PortalItem. Authenticate with the portal if required.
  2. Create an OfflineMapTask with the map.
  3. Generate default task parameters using the extent area you want to download with the OfflineMapTask.makeDefaultGenerateOfflineMapParameters(withAreaOfInterest:) method.
  4. Generate additional "override" parameters using the default parameters with the OfflineMapTask.makeGenerateOfflineMapParameterOverrides(parameters:) method.
  5. For the basemap:
    • Get the parameters OfflineMapParametersKey for the basemap layer.
    • Get the ExportTileCacheParameters for the basemap layer from GenerateOfflineMapParameterOverrides exportTileCacheParameters[key] with the key above.
    • Set the level IDs you want to download by setting the levelIDs property of ExportTileCacheParameters.
    • To buffer the extent, set a buffered geometry to the areaOfInterest property of ExportTileCacheParameters, where the buffered geometry can be calculated with the GeometryEngine.
  6. To remove operational layers from the download:
    • Create an OfflineMapParametersKey with the operational layer.
    • Use the key to obtain the relevant GenerateGeodatabaseParameters from the generateGeodatabaseParameters property of GenerateOfflineMapParameterOverrides.
    • Remove the GenerateLayerOption from the layerOptions where the option's ID matches the serviceLayerID.
  7. 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.
  8. To not crop a layer's features to the extent of the offline map (default is true):
    • Set useGeometry property of GenerateLayerOption to false.
  9. Create a GenerateOfflineMapJob with OfflineMapTask.makeGenerateOfflineMapJob(parameters:downloadDirectory:overrides:). Start the job with GenerateOfflineMapJob.start().
  10. When GenerateOfflineMapJog.output is done, get a reference to the offline map with output.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

GenerateOfflineMapWithCustomParametersView.swiftGenerateOfflineMapWithCustomParametersView.swiftGenerateOfflineMapWithCustomParametersView.Model.swiftGenerateOfflineMapWithCustomParametersView.CustomParameters.swift
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// 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()
    }
}

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