我尝试在swift和Xcode 10中使用mapBox下载不同的区域包。 这个想法是,当下载一个包时,另一个将以一个新的区域开始。 因此,为此,我编写了以下代码:
func startDowloadMapOffline(step:TravelStep){
downloadingMapOffline = true
let region = MGLTilePyramidOfflineRegion(styleURL: MGLStyle.streetsStyleURL, bounds: MGLCoordinateBounds(sw: CLLocationCoordinate2D(latitude: step.lattitudeWest, longitude: step.longitudeWest), ne: CLLocationCoordinate2D(latitude: step.lattitudeNorth, longitude: step.longitudeNorth)), fromZoomLevel: 8, toZoomLevel: 12)
// Store some data for identification purposes alongside the downloaded resources.
let userInfo = ["name\(startStepIndex)": "My Offline Pack \(startStepIndex)"]
let context = NSKeyedArchiver.archivedData(withRootObject: userInfo)
// Create and register an offline pack with the shared offline storage object.
MGLOfflineStorage.shared.addPack(for: region, withContext: context) { (pack, error) in
guard error == nil else {
// The pack couldn’t be created for some reason.
self.delegate?.downloadDidFaileWithError(with: "error download")
return
}
// Start downloading.
pack!.resume()
}
}
第一个软件包已下载,但是当我传递给第二个软件包时,pack.resume()
未启动!
在offlinePackProgressDidChange
方法中,我注意到当completedRessources
等于expectedRessources
且具有第一个包的值时,它会直接进入状态...
if completedResources == expectedResources {
let byteCount = ByteCountFormatter.string(fromByteCount: Int64(pack.progress.countOfBytesCompleted), countStyle: ByteCountFormatter.CountStyle.memory)
print("Offline pack “\(userInfo["name"] ?? "unknown")” completed: \(byteCount), \(completedResources) resources")
startStepIndex = startStepIndex + 1
delegate?.downloadStepTilesDidFinish(num: startStepIndex)
} else {
// Otherwise, print download/verification progress.
print("Offline pack “\(userInfo["name"] ?? "unknown")” has \(completedResources) of \(expectedResources) resources — \(progressPercentage * 100)%.")
}