下面是非常简单的自定义叠加对象:
@interface Overlay : NSObject <MKOverlay>
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, assign) MKMapRect boundingMapRect;
@end
@implementation Overlay
- (id)init {
self = [super init];
if (self) {
_coordinate = CLLocationCoordinate2DMake(37.78577, -122.40645);
_boundingMapRect.origin = MKMapPointForCoordinate(_coordinate);
_boundingMapRect.size = MKMapSizeMake(200, 200);
}
return self;
}
@end
OverlayView也很简单:
@implementation OverlayView
- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context
{
Overlay* overlay = (Overlay*)[self overlay];
CGRect r = [self rectForMapRect:overlay.boundingMapRect];
CGContextSetStrokeColorWithColor(context, [[UIColor redColor] CGColor]);
CGContextSetLineWidth(context, 10);
CGContextStrokeEllipseInRect(context, r);
}
@end
按钮点击后更新了其boundingMapRect
属性:
-(IBAction)onUpdateOverlayBtn:(id)sender
{
_overlay.boundingMapRect = MKMapRectInset(_overlay.boundingMapRect, -3000, -3000);
[_overlayView setNeedsDisplayInMapRect:_overlay.boundingMapRect];
}
当MKMapView放大(右侧)时出现问题: