我有一个情况,我需要导入覆盖地图顶部的MkMapView。 叠加层完全覆盖了下面的Google地图块,因此不需要加载,而且还会增加应用程序的开销。MkMapView取消加载谷歌地图
有没有办法告诉mkMapView停止加载瓷砖?
谢谢先进。
我有一个情况,我需要导入覆盖地图顶部的MkMapView。 叠加层完全覆盖了下面的Google地图块,因此不需要加载,而且还会增加应用程序的开销。MkMapView取消加载谷歌地图
有没有办法告诉mkMapView停止加载瓷砖?
谢谢先进。
其实有2种方式来实现真正的“隐藏谷歌砖”的方法(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描述。
据我所知,您无法阻止MKMapView
加载Google地图磁贴,并且如果您的应用在显示MKMapView
时掩盖了Google徽标,则可能会被拒绝 - 您可能需要考虑编写自定义UIScrollView
来代替显示您的地图。
我遇到了一个相关的问题,但在我的情况下,我的覆盖没有覆盖所有的谷歌瓷砖。 如果任何人有这个问题,并试图停止加载谷歌瓷砖,我设法叠加在谷歌地图瓷砖上的一个灰色瓷砖(256x256)覆盖。 为此,顶层tile必须为 /FakeTiles/1/1/0.png,并将此目录添加到项目资源中。 (注:不拖到这个项目>添加文件>文件夹>创建任何文件夹文件夹引用)
//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
上述方法是否也阻止下载瓷砖? – VerticalEvent 2012-06-29 12:34:58
两种方法我会回答不,可能不会。 – apouche 2012-06-29 13:20:18
好像苹果已经改变了在ios10中的实现。 _MKMapLayerHostingView https://github.com/nst/iOS-Runtime-Headers/tree/master/Frameworks/MapKit.framework – johndpope 2016-12-10 16:40:00