0

I tried implementing this answer: https://stackoverflow.com/a/22716610, to the problem of adding overlays to mkmapsnapshotter in iOS7 (cant do renderInContext method). I did this as shown below, but the image returned has only the map with no overlays. Forgive me, I am quite new to this. Thanks.

-(void)mapViewDidFinishRenderingMap:(MKMapView *)mapView fullyRendered:(BOOL)fullyRendered
{
if (mapView.tag == 100) {

    MKMapSnapshotOptions *options = [[MKMapSnapshotOptions alloc] init];
    options.region = mapView.region;
    options.size = mapView.frame.size;
    options.scale = [[UIScreen mainScreen] scale];

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

        UIImage *image = snapshot.image;

        UIGraphicsBeginImageContextWithOptions(image.size, YES, image.scale);
        {
            [image drawAtPoint:CGPointMake(0, 0)];

            CGContextRef context = UIGraphicsGetCurrentContext();

            CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
            CGContextSetLineWidth(context,5.0f);
            CGContextBeginPath(context);
            bool first = YES;
            NSArray *overlays = mapView.overlays;
            for (id <MKOverlay> overlay in overlays) {
                CGPoint point = [snapshot pointForCoordinate:overlay.coordinate];

                if(first)
                {
                    first = NO;
                    CGContextMoveToPoint(context,point.x, point.y);
                }
                else{
                    CGContextAddLineToPoint(context,point.x, point.y);

                }
            }
            UIImage *compositeImage = UIGraphicsGetImageFromCurrentImageContext();
            NSData *data = UIImagePNGRepresentation(compositeImage);
            placeToSave = data;
            NSLog(@"MapView Snapshot Saved.");

//show image for debugging
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 200, 320, 320)];
            imageView.image = compositeImage;
            [self.view addSubview:imageView];
        }
        UIGraphicsEndImageContext();
    }];

    [mapView setHidden:YES];
}
}
Community
  • 1
  • 1
Kurt J
  • 2,558
  • 24
  • 17
  • 1
    needs this line for any future idiots like me: CGContextStrokePath(context); – Kurt J Jul 01 '14 at 00:37
  • Unless I'm missing something, we appear to be missing the inner loop to step through the points on the polyLine (we just have the outer loop to get each overlay in turn, when there are several) – Peter Johnson Apr 11 '15 at 23:46

0 Answers0