MapKit Tile Overlay broken in iOS8

Spread the love

Question Description

I have this code to implement OSM in my app over default Apple one:

dispatch_async(dispatch_get_main_queue(), ^{
    NSString *template = @"http://tile.openstreetmap.org/{z}/{x}/{y}.png";
    MKTileOverlay *overlay = [[MKTileOverlay alloc] initWithURLTemplate:template];
    overlay.canReplaceMapContent = YES;
    [self.mapView addOverlay:overlay level:MKOverlayLevelAboveLabels];});

And:

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id)overlay {
if ([overlay isKindOfClass:[MKTileOverlay class]]) {
    return [[MKTileOverlayRenderer alloc] initWithTileOverlay:overlay];
}
else return nil;
}

In iOS 7 it was fine, but now it returns multiple times and the map is not loaded at all:

: ImageIO: CGImageSourceCreateWithData data parameter is nil

How can I fix it? I tried to refactor my code, so first chunk of code is now:

dispatch_queue_t fetchTiles = dispatch_queue_create("fetcher", NULL);
dispatch_async(fetchTiles, ^{
    NSString *template = @"http://tile.openstreetmap.org/{z}/{x}/{y}.png";
    MKTileOverlay *overlay = [[MKTileOverlay alloc] initWithURLTemplate:template];
    overlay.canReplaceMapContent = YES;
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.mapView addOverlay:overlay level:MKOverlayLevelAboveLabels];});});

But this doesn’t seem to fix the problem.

Practice As Follows

The code you are using looks good, except…

It seems odd that you are doing the add overlay in a block. I just add on an action and it does not seem to hang the UI at all.

just do…

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

Also, I submitted a bug to Apple and when they wanted a sample, my overlay would not show…until I set the delegate. (Even though I was wired up in storyboard). Apple closed my bug as a duplicate so they know about it.

mapView.delegate = self

Leave a Comment