MKMapSnapshotter satellite error image when camera zoom / altitude level is close in

792 Views Asked by At

I am getting an error image when I use MKMapsnapshotter. I tried manually zooming out (setting a higher camera altitude) and that seems to fix the issue, but I am not sure how to handle this programmatically. Is there a way to detect this kind of error and then reset the camera altitude? Is this a MapKit bug? This happens when I use a satellite or hybrid map type.

https://i.stack.imgur.com/bKmGI.jpg

MKMapSnapshotter *snapshotter = [[MKMapSnapshotter alloc] initWithOptions:options];
      [snapshotter startWithCompletionHandler:^(MKMapSnapshot *snapshot, NSError *error) {

if (!snapshot.image || error) {
// Can't create snapshot. Use Placeholder
   mapImage.image = [UIImage imageNamed:@"NoPhoto.png"];
   NSLog(@"Snapshot Error: %@",[error localizedDescription]);
}

Any help would be greatly appreciated. Thanks!


EDIT:

After more research and testing, I was able to figure out a workaround.

This error image was mainly happening in satellite imagery over oceans/seas. I found that I can use reverse geocoder and check the placemark array to see if the location is in a city. If not, then it is probably over an ocean.

// Check location of satellite imagery
CLLocation *location = [[CLLocation alloc] initWithLatitude:myLatitude longitude:myLongitude];
CLGeocoder *geocoder = [[CLGeocoder alloc] init];

[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray* placemarks, NSError* error){
    // check to see if location is in a city
    if ([placemarks[0] locality]) {
        // set up camera
        MKMapCamera  *myCamera = [MKMapCamera
             cameraLookingAtCenterCoordinate:location.coordinate
             fromEyeCoordinate:location.coordinate
             eyeAltitude:250];

        // set snapshot options
        MKMapSnapshotOptions *options = [[MKMapSnapshotOptions alloc] init];
        options.camera = myCamera;
        options.showsBuildings = YES;
        options.showsPointsOfInterest = NO;
        options.mapType = MKMapTypeSatellite;
        options.scale = [UIScreen mainScreen].scale;
        options.size = myImage.frame.size;

        MKMapSnapshotter *snapshotter = [[MKMapSnapshotter alloc] initWithOptions:options];
        [snapshotter startWithCompletionHandler:^(MKMapSnapshot *snapshot, NSError *error) {
            if(!error) {
                myImage.image = snapshot.image;
            }
        }]; // end snapshotter completion handler

    } else {
        // probably in the ocean
        myImage.image = [UIImage imageNamed:@"NoPhoto.png"];
    } 
}];
0

There are 0 best solutions below