MkMapView取消加载谷歌瓷砖

时间:2011-03-15 20:35:17

标签: ios mkmapview

我有一个需要在MkMapView顶部导入叠加贴图的情况。 叠加层完全覆盖了下面的谷歌图块,因此不需要加载,而且还会增加应用程序的开销。

有没有办法告诉mkMapView停止加载瓷砖?

3 个答案:

答案 0 :(得分:4)

实际上有两种方法可以实现真正的“隐藏Google磁贴”方法(johndope解决方案只在其上放置一个叠加但不会阻止加载磁贴)。

请注意,下面描述的选项可能会使您的申请被拒绝,而选项2则不会,但会更复杂。

公共部分:检索MKMapTileView对象

每个MKMapView内部都有一个未记录的类:MKMapTileView。检索它不是拒绝的理由。在此代码中,MKMapView实例将被称为mapView

UIView* scrollview = [[[[mapView subviews] objectAtIndex:0] subviews] objectAtIndex:0];
UIView* mkTiles = [[scrollview subviews] objectAtIndex:0]; // <- MKMapTileView instance

选项1:未记录的方法(!!可能是拒绝的原因!!)

if ( [mkTiles respondsToSelector:@selector(setDrawingEnabled:)])
    [mkTiles performSelector:@selector(setDrawingEnabled:) withObject:(id)NO];

这将阻止在setDrawingEnabled实例上调用未记录的方法MKMapTileView

选项2:方法调整

在控制器实现之外,你会写一些像:

// Import runtime.h to unleash the power of objective C 
#import <objc/runtime.h>

// this will hold the old drawLayer:inContext: implementation
static void (*_origDrawLayerInContext)(id, SEL, CALayer*, CGContextRef);

// this will override the drawLayer:inContext: method
static void OverrideDrawLayerInContext(UIView *self, SEL _cmd, CALayer *layer, CGContextRef context)
{
    // uncommenting this next line will still perform the old behavior
    //_origDrawLayerInContext(self, _cmd, layer, context);

    // change colors if needed so that you don't have a black background
    layer.backgroundColor = RGB(35, 160, 211).CGColor;

    CGContextSetRGBFillColor(context, 35/255.0f, 160/255.0f, 211/255.0f, 1.0f);
    CGContextFillRect(context, layer.bounds);
}

代码中的某处(加载地图视图后!):

// Retrieve original method object
Method  origMethod = class_getInstanceMethod([mkTiles class], 
                                             @selector(drawLayer:inContext:));

// from this method, retrieve its implementation (actual work done)
_origDrawLayerInContext = (void *)method_getImplementation(origMethod);

// override this method with the one you created    
if(!class_addMethod([mkTiles class],
                    @selector(drawLayer:inContext:), 
                    (IMP)OverrideDrawLayerInContext,
                    method_getTypeEncoding(origMethod)))
{
    method_setImplementation(origMethod, (IMP)OverrideDrawLayerInContext);
}

希望这有助于任何人,此代码最初在此blog post中进行了描述。

答案 1 :(得分:1)

据我所知,您无法阻止MKMapView加载Google地图图块,如果在展示MKMapView时屏幕覆盖了Google徽标,您的应用可能会被拒绝 - 您可能需要考虑编写自定义UIScrollView来显示您的地图。

答案 2 :(得分:1)

我遇到了一个相关的问题,但在我的情况下,我的叠加层并未覆盖所有谷歌瓷砖。 如果有人有这个问题,并试图停止加载谷歌瓷砖,我设法在谷歌地图瓷砖上叠加一个灰色瓷砖(256x256)覆盖。 要做到这一点,顶级磁贴必须是 /FakeTiles/1/1/0.png并将此目录添加到要投影的资源。 (N.B.不要将其拖入项目&gt;添加文件&gt;文件夹&gt;为任何文件夹创建文件夹引用)

//hack to overlay grey tiles
NSString *fakeTileDirectory = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"FakeTiles"];
TileOverlay *greyOverlay = [[TileOverlay alloc] initWithTileDirectory:fakeTileDirectory];
[mapView addOverlay:greyOverlay];
[greyOverlay release];

然后添加自定义图块叠加层。

然后您需要此代码来缩放灰色磁贴 Calculating tiles to display in a MapRect when "over-zoomed" beyond the overlay tile set