CoderNinja
CoderNinja

Reputation: 571

How do I utilize OpenLayer (OpenStreetMap) tiles in my mapview iOS 7

I started researching maps recently and wanted to know how I could implement the tiles from OpenLayer (which is what I believe openstreetmap is displaying) over my MKMapView. The default sdk doesnt show enough detail for what I want to do. I could ping the servers for the tiles but I keep seeing warning about doing so. So my two problems are 1) how do i get the tiles from the openlayer and overlay them in my mapview and 2) is there a way of getting these tiles ahead of time and just storing them in my app? I am only trying to map a small area (roughly 9 square miles worth). Any suggestions are appreciated

Upvotes: 0

Views: 1491

Answers (1)

NigelG
NigelG

Reputation: 651

What you are looking for is a MKTileOverlay which overlays the tiles from OpenSteetMap.

There is some sample code for this by Mattt Thompson on NSHipster:

static NSString * const template = @"http://tile.openstreetmap.org/{z}/{x}/{y}.png";

MKTileOverlay *overlay = [[MKTileOverlay alloc] initWithURLTemplate:template];
overlay.canReplaceMapContent = YES;

[self.mapView addOverlay:overlay
               level:MKOverlayLevelAboveLabels];

For each required tile, this will download the data from the URL template.

See http://nshipster.com/mktileoverlay-mkmapsnapshotter-mkdirections/ (where this is taken from) for more details.

If you want to manage your own tile cache (for example, some tiles are shipped along with the application), you can subclass

- (void)loadTileAtPath:(MKTileOverlayPath)path result:(void (^)(NSData *tileData, NSError *error))result

and provide the data directly. However, I'd imagine that even 9 square miles worth of data would be very large if all zoom levels are required. You could potentially have a hybrid though, and provide some data with your app, and the higher zoom levels are downloaded directly.

Upvotes: 1

Related Questions