Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear MKMapView cache?

I am trying load map region and MKMapView delegate methods are not being called on second or subsequent load. None of the delegate methods viz

- (void)mapViewWillStartLoadingMap:(MKMapView *)mapView;
- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView;
- (void)mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error;

are ever called. The Only methods called are

- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated;
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated;

It seems that ios4 is caching mapview tiles images.

I found these lines in MKMapViewDelegate Protocol Reference documentation. Highlighted line is my problem.

This method is called when the map tiles associated with the current request have been loaded. Map tiles are requested when a new visible area is scrolled into view and tiles are not already available. Map tiles may also be requested for portions of the map that are not currently visible. For example, the map view may load tiles immediately surrounding the currently visible area as needed to handle small pans by the user.

I need to perform certain operations after the map is loaded but since none of the above mentioned delegate methods are getting called I am not able to perform desired functionality. Can anyone suggest a fix to either clear the cache or provide an alternative solution for this ? I have already tried using the methods described here and this but I am still not been able to get the code working.

like image 791
Rahul Sharma Avatar asked Aug 26 '11 06:08

Rahul Sharma


2 Answers

I think you can couple willStartLoadingMap, didFinishLoadingMap and regionDidChange, like this:

  • in willStartLoadingMap set a loading flag to true;
  • in didFinishLoadingMap set the loading flag to false and also check if you have a queued call for the method that captures the screen. If so, call it;
  • in regionDidChange check the loading flag and if it's set to false, call the method that captures the screen. Otherwise, queue it so it's executed when the map finishes loading.

This way you're sure that you capture the screen after the tiles have been loaded.

However, regionDidChange may be called many times, so make sure you grab the screen only when the map view changes significantly (you can compare previous map region/center and current map region/center for this).

like image 198
AlexB Avatar answered Nov 04 '22 17:11

AlexB


I created a new project and connected the delegate to my view controller. The first two methods of the three in question gets called. Since it loads all tiles the error delegate method wasn't called.

I just recently walked into similar problems. I subclassed MKMapView and forget to set the delegate in my custom init methods and in awakeFromNib:. Perhaps that's what causing your problems, too.

like image 2
Jens Kohl Avatar answered Nov 04 '22 15:11

Jens Kohl