我非常需要为我的应用制作离线 map ,因为它主要是在泰国建立的,泰国通常很难获得互联网连接。我现在为自己的OpenStreetMap使用MKTileOverlay,但是在实现脱机使用时遇到了问题。我发现一个教程说可以将MKTileOverlay子类化。因此,在 map 所在的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);
        }];
    }
}

问题是,除非我注释掉子类中的代码,否则根本不会加载任何东西。我在哪里弄糟?

最佳答案

在我们公司中,我们选择使用MapBox进行离线映射。

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

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

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

我确实感谢您的确切问题是如何使OpenStreetMap自己的 map 脱机,但是我希望这对您有用。

10-08 18:43