0

I am using the following code to load custom map tiles into my app. When I am using the mainBundle as a path it is working as expected:

NSString *baseURL = [[[NSBundle mainBundle] bundleURL] absoluteString];
NSString *urlTemplate = [baseURL stringByAppendingString:@"/tiles/{z}/{x}/{y}.png"];
self.tileOverlay = [[MKTileOverlay alloc] initWithURLTemplate:urlTemplate];
self.tileOverlay.canReplaceMapContent=YES;
[self.mapView insertOverlay:self.tileOverlay belowOverlay:self.gridOverlay];

BUT if I try to change the path to the documents folder (that because I am planning to download the tiles folder and store them in documents folder) the following code is not working:

NSString *destinationPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *urlTemplate = [destinationPath stringByAppendingString:@"tiles/{z}/{x}/{y}.png"];

self.tileOverlay = [[MKTileOverlay alloc] initWithURLTemplate:urlTemplate];
self.tileOverlay.canReplaceMapContent=YES;
[self.mapView insertOverlay:self.tileOverlay belowOverlay:self.gridOverlay];

Any hint would be usefull!

Note: The tiles folder exists on my path. More specifically the following code returns YES

NSString* foofile = [destinationPath stringByAppendingPathComponent:@"/tiles/17/70759/49235.png"]; //as an example
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:foofile];
arniotaki
  • 2,175
  • 2
  • 23
  • 26

1 Answers1

1

I just found out why the code didn't work.

The documents path should be:

NSString *destinationPath = [[self applicationDocumentsDirectory]  absoluteString];

instead of :

NSString *destinationPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

where applicationDocumentsDirectory is:

 - (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

The following link https://stackoverflow.com/a/34543841/1465756 help me find another way to get documents path.

arniotaki
  • 2,175
  • 2
  • 23
  • 26