Skip to content

Instantly share code, notes, and snippets.

@StuartFarmer
Last active August 23, 2022 11:59
Show Gist options
  • Save StuartFarmer/b24b1e7f6d260aed8ea4 to your computer and use it in GitHub Desktop.
Save StuartFarmer/b24b1e7f6d260aed8ea4 to your computer and use it in GitHub Desktop.
Quick snippet to overlay Apple map data with OSM data, MapBox data, or any other map that has an online tile system on the MKMapView
// Import these Frameworks:
#import <MapKit/MapKit.h>
// Include this delegate
@interface ViewController () <MKMapViewDelegate>
- (void)viewDidLoad
{
// Add this code to your preexisting viewDidLoad method
NSString *template = @"http://tile.openstreetmap.org/{z}/{x}/{y}.png"; // Tile system URL template goes here
MKTileOverlay *overlay = [[MKTileOverlay alloc] initWithURLTemplate:template]; // Associating overlay with URL template
overlay.canReplaceMapContent = YES; // Allowing overlays on MKMapView & disabling Apple map data
[self.mapView addOverlay:overlay level:MKOverlayLevelAboveLabels]; // Adding overlay to MKMapView above Apple lables
self.mapView.delegate = self; // Linking the delegate to allow the next method to be called
}
// And this somewhere in your class that’s mapView’s delegate (most likely a view controller).
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {
if ([overlay isKindOfClass:[MKTileOverlay class]]) {
return [[MKTileOverlayRenderer alloc] initWithTileOverlay:overlay]; // Overlay the tile with the new template
}
return nil;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment