Utilicé el mapa predeterminado de MapKit y una subclase de MKTileOverlay para poder guardar los mosaicos descargados y devolver los mosaicos ya almacenados en caché sin descargarlos.
1) Cambio de fuente para el mapa predeterminado de MapKit y utilizar una subclase de MKTileOverlay (Used "abrir mapa de calle" aquí)
- (void)viewDidLoad{
[super viewDidLoad];
static NSString * const template = @"http://tile.openstreetmap.org/{z}/{x}/{y}.png";
VHTileOverlay *overlay = [[VHTileOverlay alloc] initWithURLTemplate:template];
overlay.canReplaceMapContent = YES;
[self.mapView addOverlay:overlay level:MKOverlayLevelAboveLabels];
}
2) subclase de MKTileOverlay
@interface VHTileOverlay() // MKTileOverlay subclass
@property (nonatomic, strong) NSOperationQueue *operationQueue;
@end
@implementation VHTileOverlay
-(instancetype)initWithURLTemplate:(NSString *)URLTemplate{
self = [super initWithURLTemplate:URLTemplate];
if(self){
self.directoryPath = cachePath;
self.operationQueue = [NSOperationQueue new];
}
return self;
}
-(NSURL *)URLForTilePath:(MKTileOverlayPath)path {
return [NSURL URLWithString:[NSString stringWithFormat:@"http://tile.openstreetmap.org/%ld/%ld/%ld.png", (long)path.z, (long)path.x, (long)path.y]];
}
-(void)loadTileAtPath:(MKTileOverlayPath)path
result:(void (^)(NSData *data, NSError *error))result
{
if (!result) {
return;
}
NSString *pathToFilfe = [[self URLForTilePath:path] absoluteString];
pathToFilfe = [pathToFilfe stringByReplacingOccurrencesOfString:@"/" withString:@"|"];
// @"/" - those are not approriate for file's url...
NSData *cachedData = [self loadFileWithName:pathToFilfe];
if (cachedData) {
result(cachedData, nil);
} else {
NSURLRequest *request = [NSURLRequest requestWithURL:[self URLForTilePath:path]];
__block VHTileOverlay *weakSelf = self;
[NSURLConnection sendAsynchronousRequest:request
queue:self.operationQueue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSLog(@"%@",[weakSelf URLForTilePath:path]);
if(data){
[self saveFileWithName:[[weakSelf URLForTilePath:path] absoluteString] imageData:data];
}
result(data, connectionError);
}];
}
}
-(NSString *)pathToImageWithName:(NSString *)fileName
{
NSString *imageFilePath = [[OfflineMapCache sharedObject].cachePath stringByAppendingPathComponent:fileName];
return imageFilePath;
}
- (NSData *)loadFileWithName:(NSString *)fileName
{
NSString *imagePath = [self pathToImageWithName:fileName];
NSData *data = [[NSData alloc] initWithContentsOfFile:imagePath];
return data;
}
- (void)saveFileWithName:(NSString *)fileName imageData:(NSData *)imageData
{
// fileName = [fileName stringByReplacingOccurrencesOfString:@"/" withString:@"|"];
// NSString *imagePath = [self pathToImageWithName:fileName];
// [imageData writeToFile:imagePath atomically:YES];
}
Descomentar " saveFileWithName "y ejecútelo en el simulador. También puede agregar NSLog (fileName) para saber dónde obtener todas las fichas que necesita. (La memoria caché del simulador está en Users/YOU/Library/Developer/CoreSimulator/Devices/... Y la biblioteca es un directorio oculto)
Después de almacenar en caché todo lo que necesita simplemente poner en el paquete de su aplicación (como cualquier otra imagen , si lo desea, desde el mapa en caché de la caja).Y dígale a su
- (void)loadTileAtPath:(MKTileOverlayPath)path
result:(void (^)(NSData *data, NSError *error))result
para obtener mosaicos del paquete.
Así que ahora puedo instalar mi aplicación, desactivar wi-fi y obtendré esos mapas de todos modos.
Me da miedo Caleb tiene razón. Incluso si pudieras de alguna manera guardar en caché los datos del mapa, no estarías de acuerdo en usarlo. –