Display Offline Maps

This section instructs how to register an offline-compatible map style, synchronize installed map data with the map engine, and switch to cache-only tile fetching when needed.

Create and register an offline-compatible style

1
final class RegionalOfflineMapViewController: UIViewController {
2
private var mapView: NGLMapView!
3
private var connectivityObserver: NSObjectProtocol?
4
5
override func viewDidLoad() {
6
super.viewDidLoad()
7
8
mapView = NGLMapView(frame: view.bounds)
9
mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
10
view.addSubview(mapView)
11
12
applyStyle(named: "Bright")
13
observeConnectivity()
14
updateTileFetchMode()
15
16
NGLRegionalOffline.refreshEngineOnMapResume(completion: nil)
17
}
18
19
private func applyStyle(named name: String) {
20
guard let styleURL =
21
NGLRegionalOfflineStyleCatalog.uri(forName: name)
22
else {
23
return
24
}
25
26
mapView.styleURL = styleURL
27
28
NGLRegionalOfflineStyleCatalog.registerMapStyle(
29
forMapView: mapView,
30
styleURL: styleURL
31
)
32
}
33
}

Read the available style names with:

1
let styleNames = NGLRegionalOfflineStyleCatalog.styleNames

The current catalog includes Bright, Dark, Satellite, SatelliteStreet, NavDay, and NavNight. Register the new URL again whenever the application changes styles.

Synchronize the map when the page appears

1
override func viewWillAppear(_ animated: Bool) {
2
super.viewWillAppear(animated)
3
4
updateTileFetchMode()
5
NGLRegionalOffline.onMapViewWillAppear()
6
}

Refresh explicitly after returning from the download page:

1
NGLRegionalOffline.refreshEngineOnMapResume { [weak self] in
2
// Installed database paths are now synchronized with the map engine.
3
self?.updateTileFetchMode()
4
}

Switch to cache-only mode when offline

1
private func observeConnectivity() {
2
connectivityObserver = NotificationCenter.default.addObserver(
3
forName: NGLRegionalOfflineNetworkConnectivityDidChangeNotification,
4
object: nil,
5
queue: .main
6
) { [weak self] _ in
7
self?.updateTileFetchMode()
8
}
9
}
10
11
private func updateTileFetchMode() {
12
mapView.regionalOfflineTileFetchCacheOnly =
13
NGLRegionalOffline.isOfflineForRegionalFallback()
14
}
15
16
deinit {
17
if let connectivityObserver {
18
NotificationCenter.default.removeObserver(connectivityObserver)
19
}
20
}

When regionalOfflineTileFetchCacheOnly is true, the map reads from shared preview resources and installed regional databases instead of requesting regional tiles online.

Focus the map on an installed region

1
let focused = NGLRegionalOffline.focusPreviewRegion(
2
with: mapView,
3
regionId: regionId
4
)
5
6
if !focused {
7
// No usable database or bounds were found for this region.
8
}