简体   繁体   中英

iOS / MapKit cache management issue with MKTileOverlay and PINCache library

I am using MapKit in order to create satellite and radar animation by adding MKTileOverlay over the mapView.

With an UISlider and a PlayButton I was able to create an animation, like a GIF by playing with the alpha of the MKOverlayRenderer (setting them to 0 or 0.75 according to the position of my slider).

The animation is quite smooth, all my satellite and radar tiles are loaded properly over the mapView.

I am encountering one issue with the cache management.

I realized that MapKit didn't use cache for my tileOverlay that's why I used the library PINCache in order to save my tiles so that it doesn't request and download the images each time I'm playing the animation.

My implementation :

I override the method URLForTilePath in order to build my URL to get my tile images.

- (NSURL *)URLForTilePath:(MKTileOverlayPath)path{

double latMin, latMax, longMin, longMax;
path.contentScaleFactor = 1.0;

NSMutableArray *result = [[NSMutableArray alloc] init];
result = getBBoxForCoordinates((int)path.x, (int)path.y, (int)path.z);

longMin = [[result objectAtIndex:0] doubleValue];
latMin = [[result objectAtIndex:1] doubleValue];
longMax = [[result objectAtIndex:2] doubleValue];
latMax = [[result objectAtIndex:3] doubleValue];

NSString *finalURL = self.url;
finalURL = [finalURL stringByReplacingOccurrencesOfString:@"DATE"
                                     withString:_date];

NSString *bbox = [NSString stringWithFormat:@"bbox=%f,%f,%f,%f", longMin, latMin, longMax, latMax];
finalURL = [finalURL stringByReplacingOccurrencesOfString:@"BBOX"
                                     withString:bbox];

return [NSURL URLWithString:finalURL];
}

And the key method that will call URLForTilePath is my implementation of loadTileAtPath :

    - (void)loadTileAtPath:(MKTileOverlayPath)path
                result:(void (^)(NSData *data, NSError *error))result
{
    if (!result)
    {
        return;
    }

    NSString *str = self.isRadar == true ? [NSString stringWithFormat:@"Radar%@", self.date] : [NSString stringWithFormat:@"Satellite%@", self.date];
    NSData *cachedData = [[PINCache sharedCache] objectForKey:str];
    if (cachedData)
    {
        result(cachedData, nil);
    }
    else
    {
        NSURLRequest *request = [NSURLRequest requestWithURL:[self URLForTilePath:path]];
        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
            NSString *str = self.isRadar == true ? [NSString stringWithFormat:@"Radar%@", self.date] : [NSString stringWithFormat:@"Satellite%@", self.date];
            [[PINCache sharedCache] setObject:data forKey:str block:nil];
            result(data, connectionError);
        }];
    }
}

Basically what I'm trying to achieve is :

  • Check if I have cached Data, if so then get the object.
  • If not, I make a request in order to download the tile with the URL given by URLForTilePath .
  • I then set the Object to the Cache.
  • The string str is my Key for the cache management
  • I have 2 important values in order to sort and differentiate the tiles, the type (Radar or Satellite, different image, different URL), and the Date.

I can see that the cache management is working, the Overlays are rendering way faster but the main issue I'm encountering is that it doesn't load and build the tiles at the according coordinate.

My mapView is like a puzzle of the world wrongly built.

With my piece of code, can you see something wrong I made ?

I couldn't find the reason to this memory/cache management issues with mapKit.

I decided to try with GoogleMap, it tooks me less than 2 hours to implement it, and the results are amazing.

I wanted to respect the Apple community by using Apple solution, but Google Map provided me so much more performances.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM