use*_*452 17 openstreetmap mapkit offline-caching ios 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];
}
Run Code Online (Sandbox Code Playgroud)
在我的子类中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);
}];
}
}
Run Code Online (Sandbox Code Playgroud)
问题是NOTHING完全被加载,除非我注释掉子类中的代码.我搞砸了哪里?
在我们公司,我们选择使用MapBox进行离线地图绘制。
在那里,您可以使用 MapBox Studio 设计和设计自己的地图,然后将地图(在选定的缩放级别范围内)导出到外部文件中。我们的大小约为 40Mb。
从那里,您可以使用MapBox iOS SDK将其轻松添加到您的应用程序中。
(免责声明:不,我不为他们工作!我们特别选择他们是因为能够定义我们自己的陆地/海洋颜色和样式,以及能够将地图文件包含在我们的 Xcode 项目中,并且可以离线使用。)
我非常感谢您的确切问题是如何使 OpenStreetMap 自己的地图离线,但我希望这对您有用。