ios - 如何使用 MapBox 读取离线 map

标签 ios swift mapbox offline

当我尝试在我的 iOS 应用程序中加载离线 mapbox map 时遇到问题。在下面的文档中,他们没有解释如何在下载后加载 map 。 https://www.mapbox.com/ios-sdk/maps/examples/offline-pack/

现在,为了加载离线 map ,我添加了这段代码,将 cache.db 文件从临时缓存复制到有效路径,如此链接中所述 https://github.com/mapbox/mapbox-gl-native/wiki/Sideloading-offline-maps

let sourceURL = NSURL(fileURLWithPath:"\(NSHomeDirectory())/Library/Application Support/*****************/.mapbox/cache.db")
        let TemporaryPathURL = NSURL(fileURLWithPath: "\(NSHomeDirectory())/Documents/***********")
        let databaseURL = TemporaryPathURL.appendingPathComponent("cache.db")
        if !(FileManager.default.fileExists(atPath: (databaseURL?.absoluteString)!)) {
            do {
                try? FileManager.default.copyItem(at: sourceURL as URL, to: databaseURL!)
            } catch {
                print ("ERROR: Fichier existant !!!!")
            }
        }

但是我没有离线获取 map 区域!!!!请帮忙

最好的问候。

最佳答案

嘿,如果你已经下载了区域(使用链接 https://www.mapbox.com/ios-sdk/maps/examples/offline-pack/ ),那么你可以试试这个代码来加载 map

///加载离线 map

func loadOffline(){

    //if app is offline load tiles
    if (MGLOfflineStorage.shared.packs?.count ?? 0) > 0{
        var index = Int()
        for i in 0..<(MGLOfflineStorage.shared.packs?.count ?? 0){
            let dict = NSKeyedUnarchiver.unarchiveObject(with: MGLOfflineStorage.shared.packs?[i].context ?? Data()) as! [String: Any]
            if (Region_Name) == JSON(dict["name"] ?? "").stringValue{
                index = i
                break
            }
        }

        mapView.styleURL =  MGLOfflineStorage.shared.packs?[index].region.styleURL
        if let tiles = MGLOfflineStorage.shared.packs?[index].region as? MGLTilePyramidOfflineRegion{

            mapView.setVisibleCoordinateBounds(tiles.bounds, animated: true)
            guard ((self.model.data?.count ?? 0) != 0) || ((self.model.mapData?.regionName ?? "") == "") else{
                return
            }
            self.mapView.zoomLevel = tiles.maximumXoomLevel
            self.mapView.centerCoordinate = CLLocationCoordinate2D(latitude: JSON(self.model.data?[0].latitude ?? "").doubleValue, longitude: JSON(self.model.data?[0].longitude ?? "").doubleValue)

        }
    }
}

关于ios - 如何使用 MapBox 读取离线 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53069026/

相关文章:

javascript - MapBox GL JS 标记偏移

ios - SwiftUI:如何以编程方式更改 rootview Controller

swift - 可以通过结构扩展 Swift 字典吗?

ios - 步骤里程碑 |里程碑听众| Mapbox iOS 等效项

ios - UIStackView 没有显示阴影

swift - 在 Swift 命令行应用程序中捕获信号

javascript - Mapbox GL JS 创建自定义图例

iOS:将字典从 plist 文件加载到数组

ios - dispatch_async(dispatch_get_main_queue() inside NSManagedObjectContext performBlock

WWDC 2012 的 ios iPaint