MKTileOverlay支持Retina瓦片

5

我在使用MKMapKit加载512x512像素的瓦片时遇到了问题。

服务器提供512x512像素的.jpeg格式瓦片。

我找不到任何解决方案或自定义视网膜瓦片在MKMapView中的示例实现。

我的做法:

当我将它们加载到MKMapView中时,使用的是

 overlay = [[MKTileOverlay alloc] initWithURLTemplate:template];
 overlay.tileSize = CGSizeMake(512.0f, 512.0f);
 [_mapView insertOverlay:overlay atIndex:MAP_OVERLAY_INDEX_TILE level:MKOverlayLevelAboveLabels];

瓷砖的缩放是正确的,但只有其中一半被加载(不仅在视觉上如此 - 我嗅探了请求,发现瓷砖确实丢失了)。

使用

 overlay = [[MKTileOverlay alloc] initWithURLTemplate:template];
 overlay.tileSize = CGSizeMake(256.0f, 256.0f);
 [_mapView insertOverlay:overlay atIndex:MAP_OVERLAY_INDEX_TILE level:MKOverlayLevelAboveLabels];

…所有的图块都显示了,但缩放不正确

这是我的绘制方法:

(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id <MKOverlay>)overlay
{
    MKOverlayRenderer *overlayRenderer = nil;

    if([overlay isKindOfClass:MKTileOverlay.class])
    {
        overlayRenderer = [[MKTileOverlayRenderer alloc] initWithTileOverlay:overlay];
    }

    return overlayRenderer;
}

无论是什么 tileSize(iOS 模拟器 7.1 retina),overlayRenderer.contentScaleFactor 总是为 1。

有什么建议吗?

祝好,Steve


你找到这个问题的解决方案了吗? - Hyndrix
1个回答

1
以下代码仅适用于iOS 7(不适用于iOS 8)。覆盖MKTileOverlayRenderer。瓷砖大小设置为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

在您的地图视图控制器中:

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

iOS 8怎么样?有什么想法吗? - Maciej Swic
不行。这个API让我很烦。我必须先忘掉这种感觉,然后再去看它。现在我在iOS 8中没有显示视网膜瓷砖。应该有一种方法可以明确地提供瓷砖大小和瓷砖比例。我不知道还有什么其他的方法可以做到这一点。 - FKDev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接