我的应用需要使用离线地图,并且使用GPX文件来规划路线。我发现OpenStreetMap可以实现此功能,但是是否有更好的服务(最好能提供等高线)?谢谢。
我的应用需要使用离线地图,并且使用GPX文件来规划路线。我发现OpenStreetMap可以实现此功能,但是是否有更好的服务(最好能提供等高线)?谢谢。
RMMapContents.m
中找到,在setTileSource:
方法内。另外还有一个在setMinZoom:
里,但我保留了最后一个。 - Alex Salom我使用了MapKit的默认地图和MKTileOverlay的子类来保存下载过的瓦片,并返回已经缓存的瓦片,而不必重新下载。
1)更改默认地图的源为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];
}
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];
}
取消“saveFileWithName”的注释并在模拟器上运行它。您还可以添加NSLog(fileName)以了解获取所有所需瓦片的位置。 (模拟器缓存位于Users / YOU / Library / Developer / CoreSimulator / Devices / ..., 而Library是一个隐藏的目录)
在缓存所有所需内容后,将其放入您的应用程序包中(就像任何其他图像一样,如果您想从盒子缓存的地图中获取)。 并告诉您的
- (void)loadTileAtPath:(MKTileOverlayPath)path
result:(void (^)(NSData *data, NSError *error))result
从bundle中获取瓦片。
现在,我可以安装我的应用程序,关闭Wi-Fi,然后无论如何都能获取这些地图。