4

下面是非常简单的自定义叠加对象:

@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时出现问题(右侧):

截屏

在 bitbucket 上测试 Xcode 项目

4

0 回答 0