【发布时间】:2016-12-16 00:28:43
【问题描述】:
我非常需要为我的应用制作离线地图,因为它主要是为泰国制作的,那里通常很难获得互联网连接。我现在正在为我的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);
}];
}
}
问题是根本没有加载任何东西,除非我注释掉子类中的代码。我在哪里搞砸了?
【问题讨论】:
-
你在哪里调用 loadTileAtPath 方法?
-
我没有调用它,因为我不确定如何调用它,因为它需要一条路径,而我不知道如何获取它。 @ldindu
-
@ldindu 我将如何传递地图最初加载到它的路径?使用正常的 initWithUrlTemplate 它只是发生了,但我只是不知道这个设置。
-
如何创建 MKTileOverlayPath 以传递给方法?
-
在[mapView addOverlay:self.overlay level:MKOverlayLevelAboveLabels];之后,需要调用[self.overlay loadTitlePath:"Your path" result: "Your completion block"]
标签: ios mapkit openstreetmap offline-caching mktileoverlay