Hiding mapview when mapoverlay is visible ios7

白昼怎懂夜的黑 提交于 2019-12-01 11:59:27
huggie

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.

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).

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)
{
    ....
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!