小智 9
我使用MapKit的默认地图和MKTileOverlay的子类来保存下载的图块并返回已经缓存的图块而不下载它们.
1)从MapKit更改默认地图的来源并使用MKTileOverlay的子类(此处使用"打开街道地图")
- (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];
}
Run Code Online (Sandbox Code Playgroud)
2)来自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];
}
Run Code Online (Sandbox Code Playgroud)
取消注释"saveFileWithName"并在模拟器上运行它.您还可以添加NSLog(fileName)以了解获取所需的所有切片的位置.(模拟器缓存位于Users/YOU/Library/Developer/CoreSimulator/Devices/...而Library是一个隐藏目录)
缓存完所有内容后,只需将应用程序包放入应用程序(就像任何其他图像一样,如果你想从盒子缓存的地图中获取).并告诉你
- (void)loadTileAtPath:(MKTileOverlayPath)path
result:(void (^)(NSData *data, NSError *error))result
Run Code Online (Sandbox Code Playgroud)
从捆绑中获取瓷砖.
所以现在我可以安装我的应用程序,关闭wi-fi,无论如何我都会获得这些地图.
查看Google 地图服务条款第 10.3 条,您会发现您不得存储任何 Google 地图内容。这意味着您不仅需要提供自己的地图,还需要替换方便的 MapKit 功能。据我所知,您确实根本无法将 MapKit 用于离线应用程序。
| 归档时间: |
|
| 查看次数: |
13682 次 |
| 最近记录: |