2011-03-15 108 views
4

我有一個情況,我需要導入覆蓋地圖頂部的MkMapView。 疊加層完全覆蓋了下面的Google地圖塊,因此不需要加載,而且還會增加應用程序的開銷。MkMapView取消加載谷歌地圖

有沒有辦法告訴mkMapView停止加載瓷磚?

謝謝先進。

回答

4

其實有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]; 

這將防止無證方法setDrawingEnabledMKMapTileView實例上調用。

選項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描述。

+0

上述方法是否也阻止下載瓷磚? – VerticalEvent 2012-06-29 12:34:58

+0

兩種方法我會回答不,可能不會。 – apouche 2012-06-29 13:20:18

+0

好像蘋果已經改變了在ios10中的實現。 _MKMapLayerHostingView https://github.com/nst/iOS-Runtime-Headers/tree/master/Frameworks/MapKit.framework – johndpope 2016-12-10 16:40:00

1

據我所知,您無法阻止MKMapView加載Google地圖磁貼,並且如果您的應用在顯示MKMapView時掩蓋了Google徽標,則可能會被拒絕 - 您可能需要考慮編寫自定義UIScrollView來代替顯示您的地圖。

1

我遇到了一個相關的問題,但在我的情況下,我的覆蓋沒有覆蓋所有的谷歌瓷磚。 如果任何人有這個問題,並試圖停止加載谷歌瓷磚,我設法疊加在谷歌地圖瓷磚上的一個灰色瓷磚(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