mirap
mirap

Reputation: 1266

MapBox cache not working

I'm trying to cache maps in MapBox. When app in online, everything works. The tileCacheDidFinishBackgroundCache is being called properly.

Then, I relaunch the app offline. Instead of map, the RMMapView shows white color only.

My code:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[RMConfiguration sharedInstance] setAccessToken:@"pk......"];

    RMTileCache  * tileCache = [[RMTileCache alloc] initWithExpiryPeriod:0];
    [tileCache setBackgroundCacheDelegate:self];

    tileSource = [[RMMapboxSource alloc] initWithMapID:@"mirap.ld8dbe2c"];    
    mapView = [[RMMapView alloc] initWithFrame:viewMapView.bounds
                                     andTilesource:tileSource];

    [mapView.tileSource setCacheable:YES];
    [viewMapView addSubview:mapView];
}


-(void)viewDidAppear:(BOOL)animated {
    RMSphericalTrapezium rect = [mapView latitudeLongitudeBoundingBox];
    mapView.tileCache.backgroundCacheDelegate = self;
    [mapView.tileCache beginBackgroundCacheForTileSource:mapView.tileSource
                                               southWest:rect.southWest
                                               northEast:rect.northEast
                                                 minZoom:10.0
                                                 maxZoom:20.0];

}
- (void)tileCacheDidFinishBackgroundCache:(RMTileCache *)tileCache {

    NSLog(@"DONE!");

}

Upvotes: 1

Views: 435

Answers (1)

incanus
incanus

Reputation: 5128

Here's what you need:

mapbox-ios-sdk-offline/ViewController.swift#L38-L44

In short, initializing a tile source with a map ID is shorthand for requesting a remote metadata URL.

-[RMMapboxSource initWithMapID:]

This method requires a network connection in order to download the TileJSON used to define the tile source.

Instead you want to also cache the tileSource.tileJSON and use it to initialize the tile source when offline.

-[RMMapboxSource tileJSON]

Useful for saving locally to use in instantiating a tile source while offline.

Upvotes: 2

Related Questions