Display an offline map (Custom)
Learn how to download and display an offline map for a user-defined geographical area of a web map.

Offline maps allow users to continue working when network connectivity is poor or lost. If a web map is enabled for offline use, a user can request that ArcGIS generates an offline map for a specified geographic area of interest.
In this tutorial, you will download an offline map for an area of interest from the web map of the stormwater network within Naperville, IL, USA. You can then use this offline map without a network connection.
To learn how to enable your web map for offline use, see the Offline enable a web map tutorial.
Prerequisites
The following are required for this tutorial:
- An ArcGIS account to access your API keys. If you don't have an account, sign up for free.
- Your system meets the system requirements.
- The ArcGIS Runtime API for iOS is installed.
Steps
Open an Xcode project
To start the tutorial, complete the Display a web map tutorial or download and unzip the solution.
Open the
.xcodeproj
file in Xcode.
Set the API Key
An API Key enables access to services, web maps, and web scenes hosted in ArcGIS Online.
Go to your developer dashboard to get your API key. For these tutorials, use your default API key. It is scoped to include all of the services demonstrated in the tutorials.
In Xcode, in the Project Navigator, click AppDelegate.swift.
In the editor, set the
apiKey
property on theAGSArcGISRuntimeEnvironment
with your API key.AppDelegate.swiftChange line // Copyright 2020 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 UIKit import ArcGIS @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Note: it is not best practice to store API keys in source code. // The API key is referenced here for the convenience of this tutorial. AGSArcGISRuntimeEnvironment.apiKey = "YOUR_API_KEY" return true } }
Get the web map item ID
You can use ArcGIS tools to create and view web maps. Use the Map Viewer to identify the web map item ID. This item ID will be used later in the tutorial.
- Go to the Naperville water network in the Map Viewer in ArcGIS Online. This web map displays stormwater network within Naperville, IL, USA.
- Make a note of the item ID at the end of the browser's URL.
The item ID should be acc027394bc84c2fb04d1ed317aac674
Display the web map
You can display a web map using the web map's item ID. Create an AGSMap
from the web map's AGSPortalItem
, and display it in your app's AGSMapView
.
In Xcode, in the Project Navigator, click ViewController.swift.
In the editor, modify the
setupMap()
function. Provide the web map's item ID.ViewController.swiftChange line 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 25 24 23 22 21 22 23 24 25 26 27 28 29 30 31 31 31 31 31 31 31 31 31 31 31 31 31 31 30 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 28 28 28 28 28 28 28 28 28// Copyright 2021 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 UIKit import ArcGIS class ViewController: UIViewController, AGSGeoViewTouchDelegate { @IBOutlet weak var mapView: AGSMapView! var offlineMapTask: AGSOfflineMapTask! var generateJob: AGSGenerateOfflineMapJob! var observation: NSKeyValueObservation? private func setupMap() { let portal = AGSPortal.arcGISOnline(withLoginRequired: false) let itemID = "acc027394bc84c2fb04d1ed317aac674" let portalItem = AGSPortalItem(portal: portal, itemID: itemID) let map = AGSMap(item: portalItem) mapView.map = map let offlineArea = AGSEnvelope(xMin: -88.1535, yMin: 41.7695, xMax: -88.1490, yMax: 41.7725, spatialReference: .wgs84()) let simpleLineSymbol = AGSSimpleLineSymbol(style: .solid, color: .red, width: 3) let simpleFillSymbol = AGSSimpleFillSymbol( style: .solid, color: .clear, outline: simpleLineSymbol) let graphic = AGSGraphic(geometry: offlineArea, symbol: simpleFillSymbol, attributes: nil) let graphicOverlay = AGSGraphicsOverlay() self.mapView.graphicsOverlays.add(graphicOverlay) graphicOverlay.graphics.add(graphic) offlineMapTask = AGSOfflineMapTask(onlineMap: map) offlineMapTask.defaultGenerateOfflineMapParameters(withAreaOfInterest: offlineArea, completion: {[weak self] (parameters, error) in guard let self = self else { return } if let error = error { print("Error fetching default parameters for the area of interest : \(error.localizedDescription)") return } guard let parameters = parameters else { return } parameters.updateMode = .noUpdates let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! let formattedDate = ISO8601DateFormatter().string(from: Date()) let downloadDirectory = documentDirectory.appendingPathComponent(formattedDate) self.generateJob = self.offlineMapTask.generateOfflineMapJob(with: parameters, downloadDirectory: downloadDirectory) let numberFormatter = NumberFormatter() numberFormatter.numberStyle = .percent self.observation = self.generateJob.progress.observe(\.fractionCompleted) { (progress, _) in print("Percentage Completed: \(numberFormatter.string(from: NSNumber(value: progress.fractionCompleted)))") } var i = 0 self.generateJob.start(statusHandler: { (_) in // Print Job messages to console. while i < self.generateJob.messages.count { print("Job message \(i): \(self.generateJob.messages[i].message)") i += 1 } }) { [weak self] (result, error) in guard let self = self else { return } if let error = error { print("Error downloading the offline map: \(error)") return } guard let result = result else { return } self.mapView.map = result.offlineMap } }) } override func viewDidLoad() { super.viewDidLoad() //AGSArcGISRuntimeEnvironment.apiKey = "YOUR-API-KEY" setupMap() } }
The remaining code creates an
AGSPortalItem
using theAGSPortal
that references ArcGIS Online, and the web map'sitemID
. TheportalItem
is used to create anAGSMap
that is displayed in the app'sAGSMapView
.Press <Command+R> to run the app.
If you are using the Xcode simulator your system must meet these minimum requirements: macOS Catalina, Xcode 11, iOS 13. If you are using a physical device, then refer to the system requirements.
You should see a map of the stormwater network within Naperville, IL, USA. Pinch, drag, and double-tap the map view to explore the web map.
Specify an area of the web map to take offline
You can specify an area of the web map to take offline using either an envelope or a polygon.
Modify the
setupMap()
function. Create anAGSEnvelope
to define the area to take offline.ViewController.swiftAdd line. 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 33 32 31 30 29 29 29 29 29 29 29 29 29 30 31 32 33 33 33 33 33 33 33 33 33 33 33 33 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -11 -12 -13 -14 -15 -16 -16 -16 -16 -16 -16 -16 -16 -16// Copyright 2021 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 UIKit import ArcGIS class ViewController: UIViewController, AGSGeoViewTouchDelegate { @IBOutlet weak var mapView: AGSMapView! var offlineMapTask: AGSOfflineMapTask! var generateJob: AGSGenerateOfflineMapJob! var observation: NSKeyValueObservation? private func setupMap() { let portal = AGSPortal.arcGISOnline(withLoginRequired: false) let itemID = "acc027394bc84c2fb04d1ed317aac674" let portalItem = AGSPortalItem(portal: portal, itemID: itemID) let map = AGSMap(item: portalItem) mapView.map = map let offlineArea = AGSEnvelope(xMin: -88.1535, yMin: 41.7695, xMax: -88.1490, yMax: 41.7725, spatialReference: .wgs84()) let simpleLineSymbol = AGSSimpleLineSymbol(style: .solid, color: .red, width: 3) let simpleFillSymbol = AGSSimpleFillSymbol( style: .solid, color: .clear, outline: simpleLineSymbol) let graphic = AGSGraphic(geometry: offlineArea, symbol: simpleFillSymbol, attributes: nil) let graphicOverlay = AGSGraphicsOverlay() self.mapView.graphicsOverlays.add(graphicOverlay) graphicOverlay.graphics.add(graphic) offlineMapTask = AGSOfflineMapTask(onlineMap: map) offlineMapTask.defaultGenerateOfflineMapParameters(withAreaOfInterest: offlineArea, completion: {[weak self] (parameters, error) in guard let self = self else { return } if let error = error { print("Error fetching default parameters for the area of interest : \(error.localizedDescription)") return } guard let parameters = parameters else { return } parameters.updateMode = .noUpdates let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! let formattedDate = ISO8601DateFormatter().string(from: Date()) let downloadDirectory = documentDirectory.appendingPathComponent(formattedDate) self.generateJob = self.offlineMapTask.generateOfflineMapJob(with: parameters, downloadDirectory: downloadDirectory) let numberFormatter = NumberFormatter() numberFormatter.numberStyle = .percent self.observation = self.generateJob.progress.observe(\.fractionCompleted) { (progress, _) in print("Percentage Completed: \(numberFormatter.string(from: NSNumber(value: progress.fractionCompleted)))") } var i = 0 self.generateJob.start(statusHandler: { (_) in // Print Job messages to console. while i < self.generateJob.messages.count { print("Job message \(i): \(self.generateJob.messages[i].message)") i += 1 } }) { [weak self] (result, error) in guard let self = self else { return } if let error = error { print("Error downloading the offline map: \(error)") return } guard let result = result else { return } self.mapView.map = result.offlineMap } }) } override func viewDidLoad() { super.viewDidLoad() //AGSArcGISRuntimeEnvironment.apiKey = "YOUR-API-KEY" setupMap() } }
This web map uses a Web Mercator spatial reference. You can define an envelope or polygon for the offline area using a different spatial reference, such as WGS84. Always ensure that the envelope's coordinate values are valid for its spatial reference.Display the area of the web map to be taken offline using a transparent graphic with a red outline.
Create an
AGSGraphic
using theofflineArea
and give it a transparentAGSSimpleFillSymbol
with a redAGSSimpleLineSymbol
outline.Create a
AGSGraphicsOverlay
and add it to the map view's collection of graphics overlays. Add thegraphic
to thegraphicsOverlay
's collection of graphics.ViewController.swiftAdd line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 35 34 33 32 31 31 31 31 31 31 31 31 31 31 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -3 -3 -3 -3 -3 -3 -3 -3// Copyright 2021 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 UIKit import ArcGIS class ViewController: UIViewController, AGSGeoViewTouchDelegate { @IBOutlet weak var mapView: AGSMapView! var offlineMapTask: AGSOfflineMapTask! var generateJob: AGSGenerateOfflineMapJob! var observation: NSKeyValueObservation? private func setupMap() { let portal = AGSPortal.arcGISOnline(withLoginRequired: false) let itemID = "acc027394bc84c2fb04d1ed317aac674" let portalItem = AGSPortalItem(portal: portal, itemID: itemID) let map = AGSMap(item: portalItem) mapView.map = map let offlineArea = AGSEnvelope(xMin: -88.1535, yMin: 41.7695, xMax: -88.1490, yMax: 41.7725, spatialReference: .wgs84()) let simpleLineSymbol = AGSSimpleLineSymbol(style: .solid, color: .red, width: 3) let simpleFillSymbol = AGSSimpleFillSymbol( style: .solid, color: .clear, outline: simpleLineSymbol) let graphic = AGSGraphic(geometry: offlineArea, symbol: simpleFillSymbol, attributes: nil) let graphicOverlay = AGSGraphicsOverlay() self.mapView.graphicsOverlays.add(graphicOverlay) graphicOverlay.graphics.add(graphic) offlineMapTask = AGSOfflineMapTask(onlineMap: map) offlineMapTask.defaultGenerateOfflineMapParameters(withAreaOfInterest: offlineArea, completion: {[weak self] (parameters, error) in guard let self = self else { return } if let error = error { print("Error fetching default parameters for the area of interest : \(error.localizedDescription)") return } guard let parameters = parameters else { return } parameters.updateMode = .noUpdates let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! let formattedDate = ISO8601DateFormatter().string(from: Date()) let downloadDirectory = documentDirectory.appendingPathComponent(formattedDate) self.generateJob = self.offlineMapTask.generateOfflineMapJob(with: parameters, downloadDirectory: downloadDirectory) let numberFormatter = NumberFormatter() numberFormatter.numberStyle = .percent self.observation = self.generateJob.progress.observe(\.fractionCompleted) { (progress, _) in print("Percentage Completed: \(numberFormatter.string(from: NSNumber(value: progress.fractionCompleted)))") } var i = 0 self.generateJob.start(statusHandler: { (_) in // Print Job messages to console. while i < self.generateJob.messages.count { print("Job message \(i): \(self.generateJob.messages[i].message)") i += 1 } }) { [weak self] (result, error) in guard let self = self else { return } if let error = error { print("Error downloading the offline map: \(error)") return } guard let result = result else { return } self.mapView.map = result.offlineMap } }) } override func viewDidLoad() { super.viewDidLoad() //AGSArcGISRuntimeEnvironment.apiKey = "YOUR-API-KEY" setupMap() } }
Press <Command+R> to run the app.
If you are using the Xcode simulator your system must meet these minimum requirements: macOS Catalina, Xcode 11, iOS 13. If you are using a physical device, then refer to the system requirements.
You should see a red rectangle on the stormwater network within Naperville, IL, USA. This is the area of the web map that you will take offline.
Dowload and display the offline map
You can generate and download an offline map for your area of interest using the AGSGenerateOfflineMapJob
class. When complete, the job will provide an offline map on your device that you can display in a map view.
Define two instance variables for
AGSOfflineMapTask
andAGSGenerateOfflineMapJob
.Instance variables ensure that objects are not deallocated while asynchronous methods are executing. If an object is deallocated while one of its asynchronous method is executing, the completion callback closure will never be called.
ViewController.swiftAdd line. Add line. 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 19 20 21 22 23 24 24 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 -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 -27 -27 -27 -27 -27 -27 -27 -27// Copyright 2021 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 UIKit import ArcGIS class ViewController: UIViewController, AGSGeoViewTouchDelegate { @IBOutlet weak var mapView: AGSMapView! var offlineMapTask: AGSOfflineMapTask! var generateJob: AGSGenerateOfflineMapJob! var observation: NSKeyValueObservation? private func setupMap() { let portal = AGSPortal.arcGISOnline(withLoginRequired: false) let itemID = "acc027394bc84c2fb04d1ed317aac674" let portalItem = AGSPortalItem(portal: portal, itemID: itemID) let map = AGSMap(item: portalItem) mapView.map = map let offlineArea = AGSEnvelope(xMin: -88.1535, yMin: 41.7695, xMax: -88.1490, yMax: 41.7725, spatialReference: .wgs84()) let simpleLineSymbol = AGSSimpleLineSymbol(style: .solid, color: .red, width: 3) let simpleFillSymbol = AGSSimpleFillSymbol( style: .solid, color: .clear, outline: simpleLineSymbol) let graphic = AGSGraphic(geometry: offlineArea, symbol: simpleFillSymbol, attributes: nil) let graphicOverlay = AGSGraphicsOverlay() self.mapView.graphicsOverlays.add(graphicOverlay) graphicOverlay.graphics.add(graphic) offlineMapTask = AGSOfflineMapTask(onlineMap: map) offlineMapTask.defaultGenerateOfflineMapParameters(withAreaOfInterest: offlineArea, completion: {[weak self] (parameters, error) in guard let self = self else { return } if let error = error { print("Error fetching default parameters for the area of interest : \(error.localizedDescription)") return } guard let parameters = parameters else { return } parameters.updateMode = .noUpdates let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! let formattedDate = ISO8601DateFormatter().string(from: Date()) let downloadDirectory = documentDirectory.appendingPathComponent(formattedDate) self.generateJob = self.offlineMapTask.generateOfflineMapJob(with: parameters, downloadDirectory: downloadDirectory) let numberFormatter = NumberFormatter() numberFormatter.numberStyle = .percent self.observation = self.generateJob.progress.observe(\.fractionCompleted) { (progress, _) in print("Percentage Completed: \(numberFormatter.string(from: NSNumber(value: progress.fractionCompleted)))") } var i = 0 self.generateJob.start(statusHandler: { (_) in // Print Job messages to console. while i < self.generateJob.messages.count { print("Job message \(i): \(self.generateJob.messages[i].message)") i += 1 } }) { [weak self] (result, error) in guard let self = self else { return } if let error = error { print("Error downloading the offline map: \(error)") return } guard let result = result else { return } self.mapView.map = result.offlineMap } }) } override func viewDidLoad() { super.viewDidLoad() //AGSArcGISRuntimeEnvironment.apiKey = "YOUR-API-KEY" setupMap() } }
Modify the
setupMap()
function to set theofflineMapTask
instance variable to a newAGSOfflineMapTask
referencing the online map.ViewController.swiftAdd line. 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 48 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 48 49 50 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 4 4 4 4 4 4 4 4// Copyright 2021 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 UIKit import ArcGIS class ViewController: UIViewController, AGSGeoViewTouchDelegate { @IBOutlet weak var mapView: AGSMapView! var offlineMapTask: AGSOfflineMapTask! var generateJob: AGSGenerateOfflineMapJob! var observation: NSKeyValueObservation? private func setupMap() { let portal = AGSPortal.arcGISOnline(withLoginRequired: false) let itemID = "acc027394bc84c2fb04d1ed317aac674" let portalItem = AGSPortalItem(portal: portal, itemID: itemID) let map = AGSMap(item: portalItem) mapView.map = map let offlineArea = AGSEnvelope(xMin: -88.1535, yMin: 41.7695, xMax: -88.1490, yMax: 41.7725, spatialReference: .wgs84()) let simpleLineSymbol = AGSSimpleLineSymbol(style: .solid, color: .red, width: 3) let simpleFillSymbol = AGSSimpleFillSymbol( style: .solid, color: .clear, outline: simpleLineSymbol) let graphic = AGSGraphic(geometry: offlineArea, symbol: simpleFillSymbol, attributes: nil) let graphicOverlay = AGSGraphicsOverlay() self.mapView.graphicsOverlays.add(graphicOverlay) graphicOverlay.graphics.add(graphic) offlineMapTask = AGSOfflineMapTask(onlineMap: map) offlineMapTask.defaultGenerateOfflineMapParameters(withAreaOfInterest: offlineArea, completion: {[weak self] (parameters, error) in guard let self = self else { return } if let error = error { print("Error fetching default parameters for the area of interest : \(error.localizedDescription)") return } guard let parameters = parameters else { return } parameters.updateMode = .noUpdates let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! let formattedDate = ISO8601DateFormatter().string(from: Date()) let downloadDirectory = documentDirectory.appendingPathComponent(formattedDate) self.generateJob = self.offlineMapTask.generateOfflineMapJob(with: parameters, downloadDirectory: downloadDirectory) let numberFormatter = NumberFormatter() numberFormatter.numberStyle = .percent self.observation = self.generateJob.progress.observe(\.fractionCompleted) { (progress, _) in print("Percentage Completed: \(numberFormatter.string(from: NSNumber(value: progress.fractionCompleted)))") } var i = 0 self.generateJob.start(statusHandler: { (_) in // Print Job messages to console. while i < self.generateJob.messages.count { print("Job message \(i): \(self.generateJob.messages[i].message)") i += 1 } }) { [weak self] (result, error) in guard let self = self else { return } if let error = error { print("Error downloading the offline map: \(error)") return } guard let result = result else { return } self.mapView.map = result.offlineMap } }) } override func viewDidLoad() { super.viewDidLoad() //AGSArcGISRuntimeEnvironment.apiKey = "YOUR-API-KEY" setupMap() } }
Get a default set of
AGSGenerateOfflineMapParameters
that you can use to generate and download the offline map.Pass the
offlineArea
to thedefaultGenerateOfflineMapParameters()
method on theofflineMapTask
. Upon completion, obtain theparameters
and set theupdateMode
to.noUpdates
. This will ensure that the offline map is read-only.This tutorial does not involve editing and updating the contents of the offline map. When an offline map is editable, metadata is stored in ArcGIS to track and synchronize edits. If you set the
updateMode
to.noUpdates
this avoids the overhead of maintaining this metadata in ArcGIS.ViewController.swiftAdd line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 50 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 50 51 52 53 54 55 56 57 58 59 60 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 60 59 58 57 56 55 54 53 52 52 52 53 53 53 53 53 53 53 53 53 53// Copyright 2021 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 UIKit import ArcGIS class ViewController: UIViewController, AGSGeoViewTouchDelegate { @IBOutlet weak var mapView: AGSMapView! var offlineMapTask: AGSOfflineMapTask! var generateJob: AGSGenerateOfflineMapJob! var observation: NSKeyValueObservation? private func setupMap() { let portal = AGSPortal.arcGISOnline(withLoginRequired: false) let itemID = "acc027394bc84c2fb04d1ed317aac674" let portalItem = AGSPortalItem(portal: portal, itemID: itemID) let map = AGSMap(item: portalItem) mapView.map = map let offlineArea = AGSEnvelope(xMin: -88.1535, yMin: 41.7695, xMax: -88.1490, yMax: 41.7725, spatialReference: .wgs84()) let simpleLineSymbol = AGSSimpleLineSymbol(style: .solid, color: .red, width: 3) let simpleFillSymbol = AGSSimpleFillSymbol( style: .solid, color: .clear, outline: simpleLineSymbol) let graphic = AGSGraphic(geometry: offlineArea, symbol: simpleFillSymbol, attributes: nil) let graphicOverlay = AGSGraphicsOverlay() self.mapView.graphicsOverlays.add(graphicOverlay) graphicOverlay.graphics.add(graphic) offlineMapTask = AGSOfflineMapTask(onlineMap: map) offlineMapTask.defaultGenerateOfflineMapParameters(withAreaOfInterest: offlineArea, completion: {[weak self] (parameters, error) in guard let self = self else { return } if let error = error { print("Error fetching default parameters for the area of interest : \(error.localizedDescription)") return } guard let parameters = parameters else { return } parameters.updateMode = .noUpdates let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! let formattedDate = ISO8601DateFormatter().string(from: Date()) let downloadDirectory = documentDirectory.appendingPathComponent(formattedDate) self.generateJob = self.offlineMapTask.generateOfflineMapJob(with: parameters, downloadDirectory: downloadDirectory) let numberFormatter = NumberFormatter() numberFormatter.numberStyle = .percent self.observation = self.generateJob.progress.observe(\.fractionCompleted) { (progress, _) in print("Percentage Completed: \(numberFormatter.string(from: NSNumber(value: progress.fractionCompleted)))") } var i = 0 self.generateJob.start(statusHandler: { (_) in // Print Job messages to console. while i < self.generateJob.messages.count { print("Job message \(i): \(self.generateJob.messages[i].message)") i += 1 } }) { [weak self] (result, error) in guard let self = self else { return } if let error = error { print("Error downloading the offline map: \(error)") return } guard let result = result else { return } self.mapView.map = result.offlineMap } }) } override func viewDidLoad() { super.viewDidLoad() //AGSArcGISRuntimeEnvironment.apiKey = "YOUR-API-KEY" setupMap() } }
Set a download directory for the offline map.
ViewController.swiftAdd line. Add line. Add line. Add line. 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 60 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 60 61 62 63 64 65 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 45 43 41 39 37 35 33 31 29 28 27 27 27 27 27 27 27 27 27 27 27// Copyright 2021 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 UIKit import ArcGIS class ViewController: UIViewController, AGSGeoViewTouchDelegate { @IBOutlet weak var mapView: AGSMapView! var offlineMapTask: AGSOfflineMapTask! var generateJob: AGSGenerateOfflineMapJob! var observation: NSKeyValueObservation? private func setupMap() { let portal = AGSPortal.arcGISOnline(withLoginRequired: false) let itemID = "acc027394bc84c2fb04d1ed317aac674" let portalItem = AGSPortalItem(portal: portal, itemID: itemID) let map = AGSMap(item: portalItem) mapView.map = map let offlineArea = AGSEnvelope(xMin: -88.1535, yMin: 41.7695, xMax: -88.1490, yMax: 41.7725, spatialReference: .wgs84()) let simpleLineSymbol = AGSSimpleLineSymbol(style: .solid, color: .red, width: 3) let simpleFillSymbol = AGSSimpleFillSymbol( style: .solid, color: .clear, outline: simpleLineSymbol) let graphic = AGSGraphic(geometry: offlineArea, symbol: simpleFillSymbol, attributes: nil) let graphicOverlay = AGSGraphicsOverlay() self.mapView.graphicsOverlays.add(graphicOverlay) graphicOverlay.graphics.add(graphic) offlineMapTask = AGSOfflineMapTask(onlineMap: map) offlineMapTask.defaultGenerateOfflineMapParameters(withAreaOfInterest: offlineArea, completion: {[weak self] (parameters, error) in guard let self = self else { return } if let error = error { print("Error fetching default parameters for the area of interest : \(error.localizedDescription)") return } guard let parameters = parameters else { return } parameters.updateMode = .noUpdates let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! let formattedDate = ISO8601DateFormatter().string(from: Date()) let downloadDirectory = documentDirectory.appendingPathComponent(formattedDate) self.generateJob = self.offlineMapTask.generateOfflineMapJob(with: parameters, downloadDirectory: downloadDirectory) let numberFormatter = NumberFormatter() numberFormatter.numberStyle = .percent self.observation = self.generateJob.progress.observe(\.fractionCompleted) { (progress, _) in print("Percentage Completed: \(numberFormatter.string(from: NSNumber(value: progress.fractionCompleted)))") } var i = 0 self.generateJob.start(statusHandler: { (_) in // Print Job messages to console. while i < self.generateJob.messages.count { print("Job message \(i): \(self.generateJob.messages[i].message)") i += 1 } }) { [weak self] (result, error) in guard let self = self else { return } if let error = error { print("Error downloading the offline map: \(error)") return } guard let result = result else { return } self.mapView.map = result.offlineMap } }) } override func viewDidLoad() { super.viewDidLoad() //AGSArcGISRuntimeEnvironment.apiKey = "YOUR-API-KEY" setupMap() } }
This tutorial code creates a new, unique folder in the device's documents folder using the current date and time.
Set the
generateJob
instance variable to a newAGSGenerateOfflineMapJob
using theparameters
and thedownloadDirectory
.ViewController.swiftAdd line. 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 65 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 65 66 67 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 49 47 45 43 41 39 37 35 33 32 31 31 31 31 31 31 31 31 31 31 31// Copyright 2021 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 UIKit import ArcGIS class ViewController: UIViewController, AGSGeoViewTouchDelegate { @IBOutlet weak var mapView: AGSMapView! var offlineMapTask: AGSOfflineMapTask! var generateJob: AGSGenerateOfflineMapJob! var observation: NSKeyValueObservation? private func setupMap() { let portal = AGSPortal.arcGISOnline(withLoginRequired: false) let itemID = "acc027394bc84c2fb04d1ed317aac674" let portalItem = AGSPortalItem(portal: portal, itemID: itemID) let map = AGSMap(item: portalItem) mapView.map = map let offlineArea = AGSEnvelope(xMin: -88.1535, yMin: 41.7695, xMax: -88.1490, yMax: 41.7725, spatialReference: .wgs84()) let simpleLineSymbol = AGSSimpleLineSymbol(style: .solid, color: .red, width: 3) let simpleFillSymbol = AGSSimpleFillSymbol( style: .solid, color: .clear, outline: simpleLineSymbol) let graphic = AGSGraphic(geometry: offlineArea, symbol: simpleFillSymbol, attributes: nil) let graphicOverlay = AGSGraphicsOverlay() self.mapView.graphicsOverlays.add(graphicOverlay) graphicOverlay.graphics.add(graphic) offlineMapTask = AGSOfflineMapTask(onlineMap: map) offlineMapTask.defaultGenerateOfflineMapParameters(withAreaOfInterest: offlineArea, completion: {[weak self] (parameters, error) in guard let self = self else { return } if let error = error { print("Error fetching default parameters for the area of interest : \(error.localizedDescription)") return } guard let parameters = parameters else { return } parameters.updateMode = .noUpdates let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! let formattedDate = ISO8601DateFormatter().string(from: Date()) let downloadDirectory = documentDirectory.appendingPathComponent(formattedDate) self.generateJob = self.offlineMapTask.generateOfflineMapJob(with: parameters, downloadDirectory: downloadDirectory) let numberFormatter = NumberFormatter() numberFormatter.numberStyle = .percent self.observation = self.generateJob.progress.observe(\.fractionCompleted) { (progress, _) in print("Percentage Completed: \(numberFormatter.string(from: NSNumber(value: progress.fractionCompleted)))") } var i = 0 self.generateJob.start(statusHandler: { (_) in // Print Job messages to console. while i < self.generateJob.messages.count { print("Job message \(i): \(self.generateJob.messages[i].message)") i += 1 } }) { [weak self] (result, error) in guard let self = self else { return } if let error = error { print("Error downloading the offline map: \(error)") return } guard let result = result else { return } self.mapView.map = result.offlineMap } }) } override func viewDidLoad() { super.viewDidLoad() //AGSArcGISRuntimeEnvironment.apiKey = "YOUR-API-KEY" setupMap() } }
To view the progress of this asynchronous job, print the fraction of the overall work completed to the Xcode console. Define an instance variable of
NSKeyValueObservation
to manage the job'sprogress
observations.ViewController.swiftAdd line. 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 19 20 21 22 23 24 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 7 5 3 1 -1 -3 -5 -7 -9 -10 -11 -11 -11 -11 -11 -11 -11 -11 -11 -11 -11// Copyright 2021 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 UIKit import ArcGIS class ViewController: UIViewController, AGSGeoViewTouchDelegate { @IBOutlet weak var mapView: AGSMapView! var offlineMapTask: AGSOfflineMapTask! var generateJob: AGSGenerateOfflineMapJob! var observation: NSKeyValueObservation? private func setupMap() { let portal = AGSPortal.arcGISOnline(withLoginRequired: false) let itemID = "acc027394bc84c2fb04d1ed317aac674" let portalItem = AGSPortalItem(portal: portal, itemID: itemID) let map = AGSMap(item: portalItem) mapView.map = map let offlineArea = AGSEnvelope(xMin: -88.1535, yMin: 41.7695, xMax: -88.1490, yMax: 41.7725, spatialReference: .wgs84()) let simpleLineSymbol = AGSSimpleLineSymbol(style: .solid, color: .red, width: 3) let simpleFillSymbol = AGSSimpleFillSymbol( style: .solid, color: .clear, outline: simpleLineSymbol) let graphic = AGSGraphic(geometry: offlineArea, symbol: simpleFillSymbol, attributes: nil) let graphicOverlay = AGSGraphicsOverlay() self.mapView.graphicsOverlays.add(graphicOverlay) graphicOverlay.graphics.add(graphic) offlineMapTask = AGSOfflineMapTask(onlineMap: map) offlineMapTask.defaultGenerateOfflineMapParameters(withAreaOfInterest: offlineArea, completion: {[weak self] (parameters, error) in guard let self = self else { return } if let error = error { print("Error fetching default parameters for the area of interest : \(error.localizedDescription)") return } guard let parameters = parameters else { return } parameters.updateMode = .noUpdates let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! let formattedDate = ISO8601DateFormatter().string(from: Date()) let downloadDirectory = documentDirectory.appendingPathComponent(formattedDate) self.generateJob = self.offlineMapTask.generateOfflineMapJob(with: parameters, downloadDirectory: downloadDirectory) let numberFormatter = NumberFormatter() numberFormatter.numberStyle = .percent self.observation = self.generateJob.progress.observe(\.fractionCompleted) { (progress, _) in print("Percentage Completed: \(numberFormatter.string(from: NSNumber(value: progress.fractionCompleted)))") } var i = 0 self.generateJob.start(statusHandler: { (_) in // Print Job messages to console. while i < self.generateJob.messages.count { print("Job message \(i): \(self.generateJob.messages[i].message)") i += 1 } }) { [weak self] (result, error) in guard let self = self else { return } if let error = error { print("Error downloading the offline map: \(error)") return } guard let result = result else { return } self.mapView.map = result.offlineMap } }) } override func viewDidLoad() { super.viewDidLoad() //AGSArcGISRuntimeEnvironment.apiKey = "YOUR-API-KEY" setupMap() } }
Observe the
fractionCompleted
property of the job'sprogress
and print it to the Xcode console in the closure callback. Assign this to theobservation
instance variable.ViewController.swiftAdd line. Add line. Add line. Add line. Add line. Add line. 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 69 70 71 72 73 74 75 76 76 75 74 73 72 71 70 69 68 67 65 63 61 59 57 55 53 51 49 48 47 47 47 47 47 47 47 47 47 47 47// Copyright 2021 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 UIKit import ArcGIS class ViewController: UIViewController, AGSGeoViewTouchDelegate { @IBOutlet weak var mapView: AGSMapView! var offlineMapTask: AGSOfflineMapTask! var generateJob: AGSGenerateOfflineMapJob! var observation: NSKeyValueObservation? private func setupMap() { let portal = AGSPortal.arcGISOnline(withLoginRequired: false) let itemID = "acc027394bc84c2fb04d1ed317aac674" let portalItem = AGSPortalItem(portal: portal, itemID: itemID) let map = AGSMap(item: portalItem) mapView.map = map let offlineArea = AGSEnvelope(xMin: -88.1535, yMin: 41.7695, xMax: -88.1490, yMax: 41.7725, spatialReference: .wgs84()) let simpleLineSymbol = AGSSimpleLineSymbol(style: .solid, color: .red, width: 3) let simpleFillSymbol = AGSSimpleFillSymbol( style: .solid, color: .clear, outline: simpleLineSymbol) let graphic = AGSGraphic(geometry: offlineArea, symbol: simpleFillSymbol, attributes: nil) let graphicOverlay = AGSGraphicsOverlay() self.mapView.graphicsOverlays.add(graphicOverlay) graphicOverlay.graphics.add(graphic) offlineMapTask = AGSOfflineMapTask(onlineMap: map) offlineMapTask.defaultGenerateOfflineMapParameters(withAreaOfInterest: offlineArea, completion: {[weak self] (parameters, error) in guard let self = self else { return } if let error = error { print("Error fetching default parameters for the area of interest : \(error.localizedDescription)") return } guard let parameters = parameters else { return } parameters.updateMode = .noUpdates let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! let formattedDate = ISO8601DateFormatter().string(from: Date()) let downloadDirectory = documentDirectory.appendingPathComponent(formattedDate) self.generateJob = self.offlineMapTask.generateOfflineMapJob(with: parameters, downloadDirectory: downloadDirectory) let numberFormatter = NumberFormatter() numberFormatter.numberStyle = .percent self.observation = self.generateJob.progress.observe(\.fractionCompleted) { (progress, _) in print("Percentage Completed: \(numberFormatter.string(from: NSNumber(value: progress.fractionCompleted)))") } var i = 0 self.generateJob.start(statusHandler: { (_) in // Print Job messages to console. while i < self.generateJob.messages.count { print("Job message \(i): \(self.generateJob.messages[i].message)") i += 1 } }) { [weak self] (result, error) in guard let self = self else { return } if let error = error { print("Error downloading the offline map: \(error)") return } guard let result = result else { return } self.mapView.map = result.offlineMap } }) } override func viewDidLoad() { super.viewDidLoad() //AGSArcGISRuntimeEnvironment.apiKey = "YOUR-API-KEY" setupMap() } }
Start the job by calling the
Start()
method. Use the job's status handler to display the work undertaken by this remote job.ViewController.swiftAdd line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 74 75 76 77 78 79 80 81 82 83 84 85 86 86 86 86 86 86 86 86 86 86 87 87 87 87 87 87 87 87 87 87 87 87// Copyright 2021 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 UIKit import ArcGIS class ViewController: UIViewController, AGSGeoViewTouchDelegate { @IBOutlet weak var mapView: AGSMapView! var offlineMapTask: AGSOfflineMapTask! var generateJob: AGSGenerateOfflineMapJob! var observation: NSKeyValueObservation? private func setupMap() { let portal = AGSPortal.arcGISOnline(withLoginRequired: false) let itemID = "acc027394bc84c2fb04d1ed317aac674" let portalItem = AGSPortalItem(portal: portal, itemID: itemID) let map = AGSMap(item: portalItem) mapView.map = map let offlineArea = AGSEnvelope(xMin: -88.1535, yMin: 41.7695, xMax: -88.1490, yMax: 41.7725, spatialReference: .wgs84()) let simpleLineSymbol = AGSSimpleLineSymbol(style: .solid, color: .red, width: 3) let simpleFillSymbol = AGSSimpleFillSymbol( style: .solid, color: .clear, outline: simpleLineSymbol) let graphic = AGSGraphic(geometry: offlineArea, symbol: simpleFillSymbol, attributes: nil) let graphicOverlay = AGSGraphicsOverlay() self.mapView.graphicsOverlays.add(graphicOverlay) graphicOverlay.graphics.add(graphic) offlineMapTask = AGSOfflineMapTask(onlineMap: map) offlineMapTask.defaultGenerateOfflineMapParameters(withAreaOfInterest: offlineArea, completion: {[weak self] (parameters, error) in guard let self = self else { return } if let error = error { print("Error fetching default parameters for the area of interest : \(error.localizedDescription)") return } guard let parameters = parameters else { return } parameters.updateMode = .noUpdates let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! let formattedDate = ISO8601DateFormatter().string(from: Date()) let downloadDirectory = documentDirectory.appendingPathComponent(formattedDate) self.generateJob = self.offlineMapTask.generateOfflineMapJob(with: parameters, downloadDirectory: downloadDirectory) let numberFormatter = NumberFormatter() numberFormatter.numberStyle = .percent self.observation = self.generateJob.progress.observe(\.fractionCompleted) { (progress, _) in print("Percentage Completed: \(numberFormatter.string(from: NSNumber(value: progress.fractionCompleted)))") } var i = 0 self.generateJob.start(statusHandler: { (_) in // Print Job messages to console. while i < self.generateJob.messages.count { print("Job message \(i): \(self.generateJob.messages[i].message)") i += 1 } }) { [weak self] (result, error) in guard let self = self else { return } if let error = error { print("Error downloading the offline map: \(error)") return } guard let result = result else { return } self.mapView.map = result.offlineMap } }) } override func viewDidLoad() { super.viewDidLoad() //AGSArcGISRuntimeEnvironment.apiKey = "YOUR-API-KEY" setupMap() } }
When the job completes, retrieve the
AGSGenerateOfflineMapResult
property. Display the offline map by passing theresult.offlineMap
property to theAGSMapView.map
.ViewController.swiftAdd line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 96 96 96 96 96 96 96 96 96 96 96// Copyright 2021 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 UIKit import ArcGIS class ViewController: UIViewController, AGSGeoViewTouchDelegate { @IBOutlet weak var mapView: AGSMapView! var offlineMapTask: AGSOfflineMapTask! var generateJob: AGSGenerateOfflineMapJob! var observation: NSKeyValueObservation? private func setupMap() { let portal = AGSPortal.arcGISOnline(withLoginRequired: false) let itemID = "acc027394bc84c2fb04d1ed317aac674" let portalItem = AGSPortalItem(portal: portal, itemID: itemID) let map = AGSMap(item: portalItem) mapView.map = map let offlineArea = AGSEnvelope(xMin: -88.1535, yMin: 41.7695, xMax: -88.1490, yMax: 41.7725, spatialReference: .wgs84()) let simpleLineSymbol = AGSSimpleLineSymbol(style: .solid, color: .red, width: 3) let simpleFillSymbol = AGSSimpleFillSymbol( style: .solid, color: .clear, outline: simpleLineSymbol) let graphic = AGSGraphic(geometry: offlineArea, symbol: simpleFillSymbol, attributes: nil) let graphicOverlay = AGSGraphicsOverlay() self.mapView.graphicsOverlays.add(graphicOverlay) graphicOverlay.graphics.add(graphic) offlineMapTask = AGSOfflineMapTask(onlineMap: map) offlineMapTask.defaultGenerateOfflineMapParameters(withAreaOfInterest: offlineArea, completion: {[weak self] (parameters, error) in guard let self = self else { return } if let error = error { print("Error fetching default parameters for the area of interest : \(error.localizedDescription)") return } guard let parameters = parameters else { return } parameters.updateMode = .noUpdates let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! let formattedDate = ISO8601DateFormatter().string(from: Date()) let downloadDirectory = documentDirectory.appendingPathComponent(formattedDate) self.generateJob = self.offlineMapTask.generateOfflineMapJob(with: parameters, downloadDirectory: downloadDirectory) let numberFormatter = NumberFormatter() numberFormatter.numberStyle = .percent self.observation = self.generateJob.progress.observe(\.fractionCompleted) { (progress, _) in print("Percentage Completed: \(numberFormatter.string(from: NSNumber(value: progress.fractionCompleted)))") } var i = 0 self.generateJob.start(statusHandler: { (_) in // Print Job messages to console. while i < self.generateJob.messages.count { print("Job message \(i): \(self.generateJob.messages[i].message)") i += 1 } }) { [weak self] (result, error) in guard let self = self else { return } if let error = error { print("Error downloading the offline map: \(error)") return } guard let result = result else { return } self.mapView.map = result.offlineMap } }) } override func viewDidLoad() { super.viewDidLoad() //AGSArcGISRuntimeEnvironment.apiKey = "YOUR-API-KEY" setupMap() } }
Press <Command+R> to run the app.
If you are using the Xcode simulator your system must meet these minimum requirements: macOS Catalina, Xcode 11, iOS 13. If you are using a physical device, then refer to the system requirements.
You should see an offline map for the specified area of the Naperville water network. Remove your network connection and you will still be able to pinch, drag, and double-tap the map view to explore this offline map.
What's next?
Learn how to use additional API features, ArcGIS platform services, and ArcGIS tools in these tutorials: