首页 > 解决方案 > 离线 MapBox 侧载合并

问题描述

第一篇到 Stacked Overflow 的帖子

我在通过侧载合并 MapBox 数据库离线内容时遇到了一些困难。我已经尝试了 GitHub 中的示例,但无济于事。

有人可以阐明我正在使用的下面的代码片段文件路径正确且可写文件大小为 66MB,因此其中有数据当我调用 MGLOfflineStorage 类的 addContents 函数时,包结果为零,内容不是合并。

有任何想法吗?

厘米

import UIKit
import Mapbox

class ViewController: UIViewController, MGLMapViewDelegate {

var mapView: MGLMapView!
var progressView: UIProgressView!

override func viewDidLoad() {
    super.viewDidLoad()

    let mapView = MGLMapView(frame: view.bounds, styleURL: MGLStyle.streetsStyleURL)
    mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    mapView.delegate = self
    view.addSubview(mapView)
    mapView.setCenter(CLLocationCoordinate2D(latitude: 22.27933, longitude: 114.16281),
                      zoomLevel: 13, animated: false)

    testAddFileContent()

    NotificationCenter.default.addObserver(self, selector: #selector(offlinePackProgressDidChange), name: NSNotification.Name.MGLOfflinePackProgressChanged, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(offlinePackDidReceiveError), name: NSNotification.Name.MGLOfflinePackError, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(offlinePackDidReceiveMaximumAllowedMapboxTiles), name: NSNotification.Name.MGLOfflinePackMaximumMapboxTilesReached, object: nil)

    print(MGLOfflineStorage.shared.packs?.count)

}

func testAddFileContent() {

    let documentPaths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
    let documentDir = documentPaths[0]
    let fileManager = FileManager.default

    let directoryExists: Bool = fileManager.fileExists(atPath: documentDir)
    if !directoryExists {
        try? fileManager.createDirectory(atPath: documentDir, withIntermediateDirectories: true, attributes: nil)
    }

    let bundle = Bundle.main

    // Valid database
    do {
        let resourceURL = bundle.url(forResource: "cache", withExtension: ".db")
        let filePath = bundle.path(forResource: "cache", ofType: ".db")

       // try? fileManager.moveItem(at: resourceURL! to: filePath!)
        let attributes = [FileAttributeKey.posixPermissions: NSNumber(value: 0o777)]
        try? fileManager.setAttributes(attributes, ofItemAtPath: filePath!)


        var fileSize : UInt64

        do {
            //return [FileAttributeKey : Any]
            let attr = try FileManager.default.attributesOfItem(atPath: filePath ?? "<#default value#>")
            fileSize = attr[FileAttributeKey.size] as! UInt64

            //if you convert to NSDictionary, you can get file size old way as well.
            let dict = attr as NSDictionary
            fileSize = dict.fileSize()
            print(fileSize)

        } catch {
            print("Error: \(error)")
        }

        MGLOfflineStorage.keyPathsForValuesAffectingValue(forKey: "packs")
        MGLOfflineStorage.shared.addContents(ofFile: filePath!, withCompletionHandler: nil)

        print(MGLOfflineStorage.shared.packs?.count)


     //   loadOffline()
    }
}

合并 MapBox 的离线侧载 cache.db

标签: swiftmapboxsideloading

解决方案


我遇到了同样的问题,终于找到了问题。在您的 main.storyboard 中单击您正在使用的地图视图,然后单击右上角的属性检查器。将默认样式 URL 更改为您在下载地图区域时使用的样式 URL。还要更改纬度、经度和缩放值以匹配您的离线区域。


推荐阅读