11

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.

Community
  • 1
  • 1
Rahul Sharma
  • 3,013
  • 1
  • 20
  • 47
  • What exactly are you trying to achieve using those delegate methods? – AlexB Sep 11 '11 at 08:09
  • I need a screen grab of map in didFinishLoadingMap, and this delegate method is only called once, after caching tiles it neither calls didfinish nor failLoad delegate. What should i do now? – Rahul Sharma Sep 12 '11 at 05:51
  • I have filed Apple Bug #13774496 related to this and created an example app to show the issue still exists on iOS6: https://github.com/iwasrobbed/MapKitDelegateBug – iwasrobbed May 01 '13 at 15:27

2 Answers2

3

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).

AlexB
  • 726
  • 4
  • 13
  • Thank you alexb. I added a small patch along with the steps you suggested and It did the trick. – Rahul Sharma Sep 13 '11 at 06:33
  • This doesn't work for the scenario where you've lost internet connection so it can't load more tiles. It still calls the delegate saying that the map has fully loaded. Seems like a bug on Apple's part. – iwasrobbed May 01 '13 at 13:20
2

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.

Jens Kohl
  • 5,899
  • 11
  • 48
  • 77