地图的离线缓存

时间:2016-08-09 20:08:41

标签: ios mapkit openstreetmap offline-caching mktileoverlay

我非常需要为我的应用做离线地图,因为它主要是为泰国制作的,因为泰国通常很难连接互联网。我现在正在为OpenStreetMap使用MKTileOverlay,但在离线使用时遇到问题。我找到了一个教程,指的是子类MKTileOverlay。所以,在我拥有地图的ViewController中:

 - (void)viewWillAppear:(BOOL)animated {

    CLLocationCoordinate2D coord = {.latitude =  15.8700320, .longitude =  100.9925410};
    MKCoordinateSpan span = {.latitudeDelta =  3, .longitudeDelta =  3};
    MKCoordinateRegion region = {coord, span};
    [mapView setRegion:region];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"Map";
    NSString *template = @"http://tile.openstreetmap.org/{z}/{x}/{y}.png";
    self.overlay = [[XXTileOverlay alloc] initWithURLTemplate:template];
    self.overlay.canReplaceMapContent = YES;
    [mapView addOverlay:self.overlay level:MKOverlayLevelAboveLabels];
}

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id)overlay {

    return [[MKTileOverlayRenderer alloc] initWithTileOverlay:overlay];
}

在我的MKTileOverlay子类中,我有:

- (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;
    }
    NSData *cachedData = [self.cache objectForKey:[self URLForTilePath:path]];
    if (cachedData) {
        result(cachedData, nil);
    } else {
        NSURLRequest *request = [NSURLRequest requestWithURL:[self URLForTilePath:path]];
        [NSURLConnection sendAsynchronousRequest:request queue:self.operationQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
            result(data, connectionError);
        }];
    }
}

问题是NOTHING完全被加载,除非我注释掉子类中的代码。我搞砸了哪里?

2 个答案:

答案 0 :(得分:1)

看起来您没有填充缓存。它总是空着的?

if (cachedData) {
    result(cachedData, nil);
} else {
    NSURLRequest *request = [NSURLRequest requestWithURL:[self URLForTilePath:path]];
    [NSURLConnection sendAsynchronousRequest:request queue:self.operationQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

        // Instantiate a new cache if we don't already have one.
        if (!self.cache) self.cache = [NSCache new];

        // Add the data into the cache so it's there for next time.
        if (data) {
            [self.cache setObject:data forKey:[self URLForTilePath:path]];
        }

        result(data, connectionError);
    }];
}

我认为这将解决您的问题。请记住,NSCache不会持久存储到磁盘(仅限内存),因此虽然它可以在应用程序背景中生存,但如果您需要完全脱机功能(可能是Core Data),那么从长远来看,您将需要更复杂的东西。

答案 1 :(得分:1)

在我们公司,我们选择使用MapBox进行离线制作。

在那里,您可以使用MapBox Studio设计和设置自己的地图,然后将地图(在选定的缩放级别范围内)导出到外部文件中。我们的大小约为40Mb。

从那里,您可以使用MapBox iOS SDK轻松将其添加到您的应用中。

(免责声明:不,我不为他们工作!我们专门选择它们来定义我们自己的陆地/海洋颜色和样式,并能够将地图文件包含在我们的Xcode项目中,并且可用脱机。)

我很感激您的确切问题是如何让OpenStreetMap自己的地图离线,但我希望这对您有用。