I have an overlay of route to be placed over a map. I have to adjust its rotation so that it overlaps exactly with the underlying map view. How can I achieve this? I have found the angle to rotate. How can I rotate my overlay view? Far till now I have added the overlay but can't rotate using transforms. What I noticed is that when I change the angle, the image is not rotating, instead it slides along x axis(side ways).
Here is the code that I tried
- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context
{
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"routeImage" ofType:@"png"];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:imagePath];
MKMapRect theMapRect = self.overlay.boundingMapRect;
CGRect theRect = [self rectForMapRect:theMapRect];
CGAffineTransform transform = CGAffineTransformMakeRotation((-35.88) / 180.0 * M_PI);
[self setTransform:transform];
CGContextSaveGState(context);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextTranslateCTM(context, 0.0, -theRect.size.height);
CGContextDrawImage(context, theRect,image.CGImage);
CGContextRestoreGState(context);
}
This is what I get
I have a workaround in which I manually rotate the image using GIMP and place it. But I need it to be customized.
This is the image transformed using GIMP and when placed on map as overlay
and this is when I rotate Context using transform [note: while rotating the context the image is rotated but is not at the exact location See image. Also the image breaks on zooming]
This is what I did
//Hayl-Road-Final.png is the actual image without transformation using GIMP
UIImage *image = [[UIImage imageNamed:@"Hayl-Road-Final.png"] retain];
CGImageRef imageReference = image.CGImage;
MKMapRect theMapRect = [self.overlay boundingMapRect];
CGRect theRect = [self rectForMapRect:theMapRect];
// We need to flip and reposition the image here
CGContextScaleCTM(ctx, 1.0, -1.0);
CGContextTranslateCTM(ctx, 0.0, -theRect.size.height);
CGContextRotateCTM(ctx,-35.88);
//drawing the image to the context
CGContextDrawImage(ctx, theRect, imageReference);