Created
March 31, 2014 23:28
-
-
Save frankus/9904654 to your computer and use it in GitHub Desktop.
MKMapSnapshot category on UIImageView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIImageView+MapSnapshot.h | |
// Awning | |
// | |
// Created by Frank Schmitt on 3/31/14. | |
// | |
#import <UIKit/UIKit.h> | |
#import <MapKit/MapKit.h> | |
@interface UIImageView (MapSnapshot) <MKAnnotation> | |
@property (strong, nonatomic) MKMapSnapshotOptions *mapSnapshotOptions; | |
@property (strong, nonatomic) MKAnnotationView *annotationView; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIImageView+MapSnapshot.m | |
// Awning | |
// | |
// Created by Frank Schmitt on 3/31/14. | |
// | |
#import "UIImageView+MapSnapshot.h" | |
#import <objc/runtime.h> | |
@implementation UIImageView (MapSnapshot) | |
@dynamic mapSnapshotOptions; | |
@dynamic annotationView; | |
- (void)setMapSnapshotOptions:(MKMapSnapshotOptions *)options { | |
objc_setAssociatedObject(self, @selector(mapSnapshotOptions), options, OBJC_ASSOCIATION_RETAIN_NONATOMIC); | |
[self.annotationView removeFromSuperview]; | |
if (options.region.span.latitudeDelta > 0.0) { | |
MKMapSnapshotter *mapSnapshotter = [[MKMapSnapshotter alloc] initWithOptions:options]; | |
[mapSnapshotter startWithCompletionHandler:^(MKMapSnapshot *snapshot, NSError *error){ | |
self.image = snapshot.image; | |
self.annotationView.bounds = CGRectMake(0.0, 0.0, self.annotationView.image.size.width, self.annotationView.image.size.height); | |
CGPoint locationPoint = [snapshot pointForCoordinate:self.coordinate]; | |
self.annotationView.center = CGPointMake(locationPoint.x + self.annotationView.centerOffset.x, locationPoint.y + self.annotationView.centerOffset.y); | |
[self addSubview:self.annotationView]; | |
}]; | |
} | |
} | |
- (MKMapSnapshotOptions *)mapSnapshotOptions { | |
return objc_getAssociatedObject(self, @selector(mapSnapshotOptions)); | |
} | |
- (CLLocationCoordinate2D)coordinate { | |
return self.mapSnapshotOptions.region.center; | |
} | |
- (void)setAnnotationView:(MKAnnotationView *)annotationView { | |
objc_setAssociatedObject(self, @selector(annotationView), annotationView, OBJC_ASSOCIATION_RETAIN_NONATOMIC); | |
} | |
- (MKAnnotationView *)annotationView { | |
MKAnnotationView *_annotationView = objc_getAssociatedObject(self, @selector(annotationView)); | |
if (!_annotationView) { | |
_annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:self reuseIdentifier:@"Annotation"]; | |
[self setAnnotationView:_annotationView]; | |
} | |
return _annotationView; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment