Loading offline maps after downloading in mapbox swift

In Mapbox I don’t know how to load offline maps. I used this https://docs.mapbox.com/ios/maps/examples/offline-pack/ documentation to download maps. Downloading is successful.

func startOfflinePackDownload() { // Create a region that includes the current viewport and any tiles needed to view it when zoomed further in. // Because tile count grows exponentially with the maximum zoom level, you should be conservative with your toZoomLevel setting. let region = MGLTilePyramidOfflineRegion(styleURL: mapView.styleURL, bounds: mapView.visibleCoordinateBounds, fromZoomLevel: mapView.zoomLevel, toZoomLevel: 14)

    // Store some data for identification purposes alongside the downloaded resources.     let userInfo = ["name": "My Offline Pack"]     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.             print("Error: \(error?.localizedDescription ?? "unknown error")")             return         }                  // Start downloading.         pack!.resume()     }           } 

this is the function I found to load maps, but self.data is unrecognisable, and I am not able to get coordinates to reload 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){                  if (Region_Name) == JSON(dict["name"] ?? "").stringValue{                     index = i                     break                 }             }         let dict = NSKeyedUnarchiver.unarchiveObject(with: MGLOfflineStorage.shared.packs?[0].context ?? Data()) as! [String: Any]         print(JSON(dict["name"]!))             if let tiles = MGLOfflineStorage.shared.packs?[0].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.maximumZoomLevel                 self.mapView.centerCoordinate = CLLocationCoordinate2D(latitude: JSON(self.model.data?[0].latitude ?? "").doubleValue, longitude: JSON(self.model.data?[0].longitude ?? "").doubleValue)             }         }     } 
Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.