MKTileOverlay with Retina-Tiles

风流意气都作罢 提交于 2019-12-04 05:36:06

The following code works only on iOS 7 (not iOS 8). Override MKTileOverlayRenderer. Tile size is set to 256.

@implementation FKDTileOverlayRenderer
-(void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context
{
    CGFloat scale = [[UIScreen mainScreen] scale];
    if (scale > 1.0)
    {
        CGSize tileSize = ((MKTileOverlay*)self.overlay).tileSize;
        CGRect rect = [self rectForMapRect:mapRect];

        CGContextSaveGState(context);
        CGAffineTransform t = CGContextGetCTM(context);
        CGContextConcatCTM(context, CGAffineTransformInvert(t));
        double ratio = tileSize.width/(rect.size.width*2);

        CGContextTranslateCTM(context, (double)(-rect.origin.x)*ratio, tileSize.height+ratio*(double)rect.origin.y);
        CGContextScaleCTM(context, ratio, -ratio);

        [super drawMapRect:mapRect zoomScale:zoomScale inContext:context];
        CGContextRestoreGState(context);
    }
    else
        [super drawMapRect:mapRect zoomScale:zoomScale inContext:context];
}
@end

In your map view controller:

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
    if ([overlay isKindOfClass:[MKTileOverlay class]]) 
    {
        return [[FKDTileOverlayRenderer alloc] initWithTileOverlay:overlay];
    }
    return nil;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!