我尝试在iOS应用中加载离线地图集地图时遇到问题。在下面的文档中,他们没有解释下载后如何加载地图。 https://www.mapbox.com/ios-sdk/maps/examples/offline-pack/
现在,为了加载离线地图,我添加了这个代码,将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 !!!!")
}
}
但我没有让地图区域离线!!!!请帮忙
最好的祝福。
嘿,如果您已经下载了区域(使用链接https://www.mapbox.com/ios-sdk/maps/examples/offline-pack/),那么您可以尝试使用此代码加载地图
///加载离线地图
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)
}
}
}