【发布时间】:2016-08-09 16:22:21
【问题描述】:
我非常需要为我的应用制作离线地图,因为它主要是为泰国制作的,那里通常很难获得互联网连接。我现在正在为我的MKTileOverlay 使用 OpenStreetMap,但在实现它以供离线使用时遇到问题。我找到了一个说子类 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);
}];
}
}
问题是根本没有加载任何东西,除非我注释掉子类中的代码。我在哪里搞砸了?
【问题讨论】:
-
您需要将所有的图块存储在本地。
-
@rmaddy 我想是这样的。你知道我该怎么做吗?
-
@rmaddy 我在nshipster.com/mktileoverlay-mkmapsnapshotter-mkdirections 找到了一些关于子类化 MKTileOverlay 的教程,但我仍然遇到问题。你能帮忙聊天吗?
-
@rmaddy 请检查帖子以获取更新。
标签: ios mkmapview mapkit offline-caching mktileoverlay