Tôi đã sử dụng bản đồ mặc định từ MapKit và một lớp con của MKTileOverlay để có thể lưu các ô đã tải xuống và trả lại các ô đã được lưu vào bộ nhớ cache mà không cần tải xuống chúng.
1) Thay đổi nguồn cho bản đồ mặc định của bạn từ MapKit và sử dụng một lớp con của MKTileOverlay (sử dụng "mở bản đồ đường phố" ở đây)
- (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) lớp con từ 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];
}
Bỏ ghi chú " saveFileWithName "và chạy nó trên trình mô phỏng. Bạn cũng có thể thêm NSLog (tên tệp) để biết nơi cần lấy tất cả các ô mà bạn cần. (Bộ nhớ cache giả lập nằm trong Users/YOU/Library/Developer/CoreSimulator/Devices/... Và Thư viện là một thư mục ẩn)
Sau khi lưu trữ tất cả những gì bạn cần chỉ cần đưa vào gói ứng dụng của bạn (giống như bất kỳ hình ảnh nào khác , nếu bạn muốn từ bản đồ lưu trong hộp).Và thông báo cho bạn
- (void)loadTileAtPath:(MKTileOverlayPath)path
result:(void (^)(NSData *data, NSError *error))result
để nhận các lát từ gói.
Vì vậy, bây giờ tôi có thể cài đặt ứng dụng của mình, tắt wi-fi và tôi sẽ vẫn nhận những bản đồ đó.
Nguồn
2015-08-27 09:01:33
Tôi e rằng Caleb đúng. Ngay cả khi bạn bằng cách nào đó có thể lưu trữ dữ liệu bản đồ - bạn sẽ không sử dụng thuật ngữ đó. –