I'm trying to display an image over a map view in iOS 7.
I subclassed MKOverlayRenderer as follows:
MapOverlayRenderer.h
#import <Foundation/Foundation.h>
@import MapKit;
@interface MapOverlayRenderer : MKOverlayRenderer
@end
MapOverlayRenderer.m
#import "MapOverlayRenderer.h"
@implementation MapOverlayRenderer
- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context {
UIImage *image = [UIImage imageNamed:@"marker"];
CGImageRef imageReference = image.CGImage;
MKMapRect theMapRect = [self.overlay boundingMapRect];
CGRect theRect = [self rectForMapRect:theMapRect];
CGContextScaleCTM(context, 1.0, -1.0);
CGContextTranslateCTM(context, 0.0, -theRect.size.height);
CGContextDrawImage(context, theRect, imageReference);
}
@end
And I have the following in my view controller which contains the map view:
ViewController.m
#import "ViewController.h"
#import "MapOverlayRenderer.h"
@import MapKit;
@interface ViewController () <MKMapViewDelegate>
@property (weak,nonatomic) IBOutlet MKMapView *mapView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.mapView setDelegate:self];
}
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {
MapOverlayRenderer *mapOverlay = [[MapOverlayRenderer alloc] init];
return mapOverlay;
}
- (IBAction)showSeattle:(id)sender {
CLLocationCoordinate2D location = {47.61167908,-122.33325958};
int radius = 100000; //radius in meters
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(location, radius, radius);
[self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
}
@end
When the IBAction
is called to zoom the map to a location, I do not see the image on top of the map view.
How can you use the MKOverlayRenderer
in iOS 7 to display images on top of a map view?