离线MapBox侧载合并

时间:2019-04-02 08:24:12

标签: swift mapbox sideloading

堆积式溢流的第一篇文章

在通过侧面加载合并MapBox数据库离线内容时,我遇到了一些困难。我没有在GitHub上尝试过这些示例。

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

有什么想法吗?

CM

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的合并

1 个答案:

答案 0 :(得分:0)

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