1

How do I hide the mapview when I have an overlay on top of the mapview in iOS7? This snippet of code used to work in iOS6 but when i upgrade my app to iOS7 it cease to work.

NSArray *views = [[[self.mapView subviews] objectAtIndex:0] subviews];

[[views objectAtIndex:0] setHidden:YES];

Any suggestions or feedback?

huggie
  • 15,393
  • 21
  • 74
  • 128
John Cruz
  • 23
  • 6

3 Answers3

2

With what incanus said with MKTileOverlay, it is like this in the view controller:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *tileTemplate = @"http://tile.stamen.com/watercolor/{z}/{x}/{y}.jpg";
    MKTileOverlay *overlay = [[MKTileOverlay alloc] initWithURLTemplate:tileTemplate];
    overlay.canReplaceMapContent = YES;
    [self.mapView addOverlay:overlay];

    [self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(37.54827, -121.98857)];
    self.mapView.delegate = self;
}


-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
    MKTileOverlayRenderer *renderer = [[MKTileOverlayRenderer alloc] initWithOverlay:overlay];
    return renderer;
}

If you need control over how the overlay feeds the data, you need to subclass MKTileOverlay and override loadTileAtPath:result:

-(void)loadTileAtPath:(MKTileOverlayPath)path result:(void (^)(NSData *, NSError *))result
{
    NSData *tile = [self someHowGetTileImageIntoNSDataBaseOnPath:path];
    if (tile) {
        result(tile, nil);
    } else {
        result(nil, [NSError errorWithDomain: CUSTOM_ERROR_DOMAIN code: 1 userInfo:nil]);
    }
}

The MKOverlay protocol requires boundingMapRect:, which should returns MKMapRect for the rectangular region that this overlay covers. However, I personally found that if I override it myself, it voids the prior canReplaceMapContent = YES setting as Apple probably does not like to show a blank gray map. So I just let MKTileMapOverlay handles it instead.

If your overlay is not actually tiles, then MKTileOverlay does not really apply. But I think you probably can fake it but always reporting nil data within loadTileAtPath:result:, and add your real overlay via another overlay. Another option would be just cover the whole world with black polygon overlay, but then the unsuspecting user would possibly be unknowingly streaming more data than he/she likes.

Community
  • 1
  • 1
huggie
  • 15,393
  • 21
  • 74
  • 128
1

MapKit isn't really designed for direct access to the map view subviews outside of true overlays (e.g. turning off Apple's map underneath).

Two ideas:

  1. Consider using the new iOS 7 MKTileOverlay class along with the canReplaceMapContent property. This has the effect of turning off Apple's underlying map.

  2. Consider a similar but separate library such as the MapBox iOS SDK which can emulate the look of MapKit but has greater flexibility for styling (and also supports back to iOS 5).

incanus
  • 4,992
  • 1
  • 11
  • 19
  • BTW I've found that canReplaceMapContent sometimes still bring out Apple map. This can happen when implementing a custom MKTileOverlay which returns boundingMapRect, which I don't know if it matters but it's a very small region compares to the world map. In that case the map region not covered by the overlay still gets displayed. – huggie Nov 05 '13 at 04:42
0

I have no idea why you would want to do it but instead of counting the number of subviews, you should just ask the mapView for the number of overlays it has

if ([[mapView overlays] count] > 0)
{
    ....
}
Craig
  • 7,729
  • 8
  • 37
  • 69
  • Im trying to hide the underlying map if there is an overlay. – John Cruz Oct 29 '13 at 09:59
  • ah, well that's different to "hiding the mapView" – Craig Oct 29 '13 at 19:24
  • Is there a way to do this without using MapBox? I need to remove the underlying map background and just display my overlay. either that or display the map region that contains the overlay and not go past that. – John Cruz Oct 30 '13 at 02:32
  • Follow what @incanus said and implement your own MKTileOverlay. Set it to replace the map content and always return a the same blank image. – Craig Oct 30 '13 at 02:35
  • can you give me example on how to use MKTileOverlay and how to return the same blank image? I cannot find any good examples on how to do that. Thank you so much Craig. – John Cruz Oct 30 '13 at 02:38
  • ok. I will post here once i figured out how to use MKTileOverlay. – John Cruz Oct 30 '13 at 03:26