Andrew
Andrew

Reputation: 7883

MKMapSnapshotter in MKTileOverlay appears displaced

I'm overriding the loadTileAtPath:result: method of MKTileOverlay with the following code. It simply converts the MKTileOverlayPath into an MKMapRect, and then uses MKMapSnapshotter to take a snapshot of the map area that's being looked at. From there, I'd do further work using the image of the map. But for now, I'm just returning the snapshots as the resulting data, so I can see how well they represent the map.

- (void)loadTileAtPath:(MKTileOverlayPath)path
                result:(void (^)(NSData *data, NSError *error))result
{
    double divisions = pow(2, (float)path.z);

    MKMapSize tileSize = MKMapSizeMake(MKMapSizeWorld.width / divisions, MKMapSizeWorld.height / divisions);

    MKMapPoint tilePoint = MKMapPointMake(path.x * tileSize.width, path.y * tileSize.height);

    MKMapRect tileRect = MKMapRectMake(tilePoint.x, tilePoint.y, tileSize.width, tileSize.height);
    MKMapSnapshotOptions *options = [[MKMapSnapshotOptions alloc] init];
    options.mapRect = tileRect;
    options.size = CGSizeMake(tileSize.width, tileSize.height);
    options.showsBuildings = NO;
    options.showsPointsOfInterest = NO;
    options.mapType = MKMapTypeStandard;
    MKMapSnapshotter *snapshotter = [[MKMapSnapshotter alloc] initWithOptions:options];
    [snapshotter startWithCompletionHandler:^(MKMapSnapshot *snapshot, NSError *error) {
        result(UIImagePNGRepresentation(snapshot.image), nil);
    }];
}

Given that the math here is all correct, in theory it should display a perfect map. Instead, it displays this:

enter image description here

And this is what it should look like (what the map below looks like):

enter image description here

Upvotes: 3

Views: 456

Answers (1)

Karsten
Karsten

Reputation: 2433

The "Bad MKMapSnapshotOptions" message is likely due to incorrect size in the options. The size cannot be too big (it seems 5000px is ok, but 1000px is too big). The size also cannot have sub-pixel sizes, so 123.4px is invalid. Make sure you use round() when you produce the size.

Upvotes: 0

Related Questions