Offline maps

The MapKit SDK downloads maps over the internet so they can be shown on user devices. Using an offline caching process, the MapKit SDK downloads maps in advance so they can be used later when there's no internet connection.

The MapKit SDK's offline mode comes with the following features:

  • Displaying map tiles.
  • Building routes for cars (without traffic data).
  • Geosuggest and search.

Warning

Offline map functionality is only available with a paid license.

Regions

In the MapKit SDK, maps are divided into regions, each of which is associated with a set of data you can download and cache offline.

Regions can be:

  • Cities (Moscow, Minsk, Kazan).
  • City regions that include multiple localities (Moscow Oblast, Minsk Region).
  • Countries (Serbia, France, Armenia).

In the MapKit SDK, regions are described by a YMKOfflineCacheRegion class and provide the following data:

  • id: The region's unique ID.
  • name: The region's name.
  • center: The coordinates of the region center.
  • size: The download size of the region (in bytes).
  • releaseTime: The timestamp for when offline maps for this region were last updated on the server.
  • parentId: The ID of the parent region. For example, Moscow as the parent region contains Moscow Oblast.

Getting a list of regions

The YMKOfflineCacheManager class is the entry point for the offline cache management API. An instance of this class is stored as a singleton in the MapKit SDK. To get it, use the YMKMapKit.offlineCacheManager method.

let offlineCacheManager = YMKMapKit.sharedInstance().offlineCacheManager

To get the list of all available regions, use the subscription to the region list update event in the YMKOfflineMapRegionListUpdatesListener interface.

class RegionListUpdatesListener: NSObject, YMKOfflineMapRegionListUpdatesListener {
    func onListUpdated() {
        let regions = offlineCacheManager.regions()
        // Handle regions update
    }
}

let regionListUpdatesListener = RegionListUpdatesListener()
offlineCacheManager.addRegionListUpdatesListener(with: regionListUpdatesListener)

You can get a list of updated regions using the YMKOfflineCacheManager.regions() method.

Region state

Region states are described by the YMKOfflineCacheRegionState list:

  • available: The region's offline maps are available for download.
  • downloading: Offline maps are being downloaded.
  • paused: The download was paused and can be resumed.
  • completed: The data is downloaded.
  • outdated: The server has an updated version of the offline maps for this region.
  • unsupported: The offline maps of the region are downloaded but no longer supported. If they are deleted from the device, the region will no longer be available for download.
  • need_update: Data outdated, download the new version.

To get the current state for a particular region, use the YMKOfflineCacheManager.getStateWithRegionId(_:) method.

You can use the MapKit SDK to subscribe to region state change events in the YMKOfflineCacheRegionListener interface. There are two options:

  1. YMKOfflineCacheRegionListener.onRegionStateChanged(withRegionId:): A particular region's state change.
  2. YMKOfflineCacheRegionListener.onRegionProgress(withRegionId:): A state change for a region's offline map.
class RegionListener: NSObject, YMKOfflineCacheRegionListener {
    func onRegionStateChanged(withRegionId regionId: UInt) {
        let state = offlineCacheManager.getStateWithRegionId(regionId)
        // Handle region state changes
    }

    func onRegionProgress(withRegionId regionId: UInt) {
        let progress = offlineCacheManager.getProgressWithRegionId(regionId)
        // Handle region progress changes
    }
}

let regionListener = RegionListener()
offlineCacheManager.addRegionListener(regionListener)

Additional information about regions

You can get additional information about regions using the following methods:

Downloading maps

The MapKit SDK provides the following methods for managing the download state of a region's offline maps.

Tip

Before downloading, use the YMKOfflineCacheManager.mayBeOutOfAvailableSpace(withRegionId:) method to make sure there's enough free space on the device.

All methods for managing offline map download states take a single argument: the ID of the region the action applies to. The region state may change when a method is executed. For example, if a region is available for download, the download starts after calling the YMKOfflineCacheManager.startDownload(withRegionId:) method, and the region's state changes to downloading.

Downloading properties

These methods let you configure offline map download properties:

Error handling

Offline map data is downloaded from the server, and offline caches are saved to the device. The YMKOfflineCacheManagerErrorListener interface is used to handle errors that may appear in the process.

class ErrorListener: NSObject, YMKOfflineCacheManagerErrorListener {
    func onErrorWithError(_ error: Error) {
        switch error {
        case let localError as YRTLocalError:
            // Handle local error
        case let remoteError as YRTRemoteError:
            // Handle remote error
        default:
            // Undefined error
        }
    }

    func onRegionErrorWithError(_ error: Error, regionId: UInt) {
        // Handle error for region with regionId id
    }
}

This interface provides methods for handling errors related to downloading particular regions and YMKOfflineCacheManager.

There are two types of errors:

Storing offline caches

The MapKit SDK saves maps downloaded to the device's disk in a special offline cache format.

To get the path for the offline cache storage in the file system, the YMKOfflineCacheManager.requestPath(pathGetterListener:) method is used.

offlineCacheManager.requestPath { path in
    // Handle requested path
}

This method takes the YMKOfflineCacheManagerPathGetterListener interface used to subscribe to the event where the path is obtained as an argument.

The cache storage on the device may be changed. There are two methods used for that:

  1. YMKOfflineCacheManager.setCachePathWithPath(_:pathSetterListener:): Used to change the location of cached data on the user's device. If there are already caches stored at the path set, they will be used, with new caches initiated otherwise.
  2. YMKOfflineCacheManager.moveData(withNewPath:dataMoveListener:): Moves current caches to a new location on the device.

The YMKOfflineCacheManager.computeCacheSize(sizeCallback:) method calculates how much memory all caches are taking up on the user's device.

The YMKOfflineCacheManager.clear(clearCallback:) method is used to delete caches.

Source code

As an example of using the offline maps API functionality, see the MapOffline app in our GitHub repository.

Previous