I used the default map from MapKit and a subclass of MKTileOverlay to be able to save downloaded fragments and return already cached fragments without loading them.
1) Change the source for your default map from MapKit and use the MKTileOverlay subclass (here an "open street map" is used)
- (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) subclass from 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:@"|"];
Uncomment "saveFileWithName" and run it on the simulator. You can also add NSLog (file_name) to find out where to get all the tiles you need. (The simulator cache is in User / YOU / Library / Developer / CoreSimulator / Devices / ... And the library is a hidden directory)
After you have cached everything you need, just add to your application suite (like any other image, if you want, using the cache card). And say
- (void)loadTileAtPath:(MKTileOverlayPath)path result:(void (^)(NSData *data, NSError *error))result
to get fragments from the package.
So, now I can install the application, turn off Wi-Fi, and still get these cards.
Vova Kalinichenko
source share