MKMapSnapshotter startWithCompletionHandler never called

1k views Asked by At

Is there any reason why the completion handler on MKMapSnapshotter's startWithCompletionHandler method would not get called back? (not even with error!!)

I'm not doing anything fancy, just grabbing a snapshot and displaying it, all in the foreground, no background processing.

What's unusual is that it seems totally random, meaning sometimes it works just fine, other times it never does.

Here's my code:

MKMapSnapshotOptions *options = [[MKMapSnapshotOptions alloc] init];
options.size = somePredeterminedSize;
options.scale = [[UIScreen mainScreen] scale];
options.mapType = MKMapTypeStandard;
options.region = MKCoordinateRegionMakeWithDistance(mycoordinate, 1000, 1000);

MKMapSnapshotter *snapshotter = [[MKMapSnapshotter alloc] initWithOptions:options];
[snapshotter startWithCompletionHandler:^(MKMapSnapshot *snapshot, NSError *error) {
    if (error) {
        NSLog(@"[Error] %@", error);
        return;
    }

    MKAnnotationView *pin = [[MKPinAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:nil];
    pin.image = [UIImage imageNamed:@"map-pin"];
    UIImage *image = snapshot.image;
    UIGraphicsBeginImageContextWithOptions(image.size, YES, image.scale);
    [image drawAtPoint:CGPointMake(0.0f, 0.0f)];
    CGRect rect = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);

    CGPoint point = [snapshot pointForCoordinate:self.bureau.coordinate];
    if (CGRectContainsPoint(rect, point)) {
        point.x = point.x + pin.centerOffset.x - (pin.bounds.size.width / 2.0f);
        point.y = point.y + pin.centerOffset.y - (pin.bounds.size.height / 2.0f);
        [pin.image drawAtPoint:point];
    }

    UIImage *compositeImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

}];

I have tested this on several devices simultaneously each running different iOS versions: iPhone 5S running iOS 8.4 iPhone 6 running iOS 9.2 iPhone 6+ running iOS 8.2

All 3 devices are connected to the same WIFI. The iPhone 6+ almost never fails while the iPhone 6 almost always does.

Any suggestions?

2

There are 2 answers

0
Oleg Sherman On

I had the same issue, in my app i have a list of addresses with some map images , and if you press on the image one of the options is to open it in the Maps App. On the iPhone it created the images perfectly but on the iPad it didn't until i tried to open the address in the Apple "Maps" App (with MKMapItem.openMapsWithItems ...), it opened the Maps App and presented an alert for permissions , i pressed "Allow" , got back to my App and it started working perfectly.

Try to open the Maps App , allow permissions , and see if it work in your app.

Hope it helps.

2
DrMickeyLauer On

I have this sometimes as well. My theory is that the "tile server" (or whatever daemon is responsible for downloading the map tiles) has crashed in that case. (Even the MKMapView instances I used did not show any tiles)

Rebooting the device always fixes it for me.