How to disable base Google Maps in MapKit

Sept 26th 2012 update :for erasing iOS6 base maps look here: https://blog.sumbera.com/2012/09/26/how-to-erase-ios6-maps-in-mapkit/ 

I have searched this and couldn’t find any answer, so here is my own research so far on this subject. You can disable MapKit base layers using 3 approaches:

#1 remove subview from MKMapView (quite bad as you will loose all overlays too)

#2 use undocumented function (bad too as this will be rejected by Apple approval process):

// get MKMapTileView from view hierarchy
UIView * mkMapTileView = [((UIView*) [ ((UIView*)[self.subviews objectAtIndex:0]).
subviews objectAtIndex:0]).subviews objectAtIndex:0];
// call undocumented method
if ( [mkMapTileView respondsToSelector:@selector(setDrawingEnabled:)]){
[mkMapTileView performSelector:@selector(setDrawingEnabled:) withObject:(id) NO];

#3 use method swizzle

inspired by http://www.mikeash.com/pyblog/friday-qa-2010-01-29-method-replacement-for-fun-and-profit.html

Making UIToolbar and UINavigationBar’s background totally transparent

this method is fine as it is official method and enables you to ‘subclass’ a class that you don’t have access in compile time.

#import <objc/runtime.h>

// original method declaration
static void (*_origDrawLayerInContext)(id, SEL, CALayer*, CGContextRef);
// set up subclass in runtime..in some entering method
UIView * mkMapTileView = [((UIView*) [ ((UIView*)[self.subviews objectAtIndex:0]). subviews objectAtIndex:0]).subviews objectAtIndex:0];

Method  origMethod = class_getInstanceMethod([mkMapTileView class], @selector(drawLayer:inContext:));

_origDrawLayerInContext = (void *)method_getImplementation(origMethod);

if(!class_addMethod([mkMapTileView class], @selector(drawLayer:inContext:), (IMP)OverrideDrawLayerInContext, method_getTypeEncoding(origMethod)))

method_setImplementation(origMethod, (IMP)OverrideDrawLayerInContext);

// implement our Override
static void OverrideDrawLayerInContext(UIView *self, SEL _cmd, CALayer *layer, CGContextRef context) {
// possibly call original method if you leave it empty base google maps will not be displayed. You can draw custom content here as well...
//  _origDrawLayerInContext(self, _cmd, layer, context);

}

This entry was posted in iPhone, Objective-C and tagged , , , . Bookmark the permalink.

4 Responses to How to disable base Google Maps in MapKit

  1. Pingback: How to erase iOS6 maps in MapKit « Geospatial Haiku

  2. Anas says:

    I tried you solution here.
    It worked great, but I cannot see the blue dot showing my location anymore. Is there any reason for that?

    • Stanislav says:

      yes, you can either make your own or try to postpone call of showsUserLocation e.g. like this:

      [[NSOperationQueue mainQueue] addOperationWithBlock:^{
      self.showsUserLocation = YES;

      }];

      • Anas says:

        Thanks for your reply.

        I couldn’t get it to work. I spent some time reveiwing NSOperationQueue but I still did not know where I should add this line for code.
        Could you clarify a bit more? Thanks.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s