1
votes

I have a problem when I try to load an offline mapbox map in my iOS app. In the documentation below, they don't explain how to load the map after downloading. https://www.mapbox.com/ios-sdk/maps/examples/offline-pack/

Now, in order to load the offline map I added this code that copy the cache.db file from the temporary cache to the valid path as described in this link 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 !!!!")
            }
        }

but I didn't get the map region offline!!!! any help please

Best regards.

1
any idea pleaseWalid Sassi

1 Answers

1
votes

Hey if you have downloaded region (using link https://www.mapbox.com/ios-sdk/maps/examples/offline-pack/), then you can try this code to load the map

///load offline 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)

        }
    }
}