As of iOS 7, MapKit includes a MKTileOverlay
class that can be used to efficiently replace Apple Maps data with custom tiles. What’s more, MKTileOverlay
’s initializer accepts URL templates with x, y, and z parameters, which are implemented by many mapping services that use Mercator projection. This makes it trivial for third-party mapping software to leverage MapKit as a renderer for custom tile data.
That’s great, but custom tile overlays aren’t just useful for terrestrial imagery. Google Sky tiles can be accessed at URLs of format:
https://mw1.google.com/mw-planetary/sky/skytiles_v1/{x}_{y}_{z}.jpg
Sure enough, we can use MapKit to render Google Sky imagery in just 12 lines of code:
- (void)awakeFromNib {
MKMapView *mv = [[MKMapView alloc] initWithFrame:[UIScreen mainScreen].bounds];
mv.delegate = self;
MKTileOverlay *overlay = [[MKTileOverlay alloc] initWithURLTemplate:
@"https://mw1.google.com/mw-planetary/sky/skytiles_v1/{x}_{y}_{z}.jpg"];
overlay.canReplaceMapContent = YES;
[mv addOverlay:overlay level:MKOverlayLevelAboveLabels];
[self.view addSubview:mv];
}
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {
return [[MKTileOverlayRenderer alloc] initWithTileOverlay:overlay];
}
Under the hood, MapKit will now load Google Sky tiles on demand as the user pans and zooms.
The complete project can be found on GitHub.
Disclaimer: This project was created for experimental purposes. Google Sky data should not be used in shipping code.