MKMapView not updating overlay correctly after changing its boundingMapRect

本秂侑毒 提交于 2019-12-23 13:53:14

问题


Below is very simple custom overlay object:

@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 is also very straightforward:

@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

And its boundingMapRect property updated on button click:

-(IBAction)onUpdateOverlayBtn:(id)sender
{
    _overlay.boundingMapRect = MKMapRectInset(_overlay.boundingMapRect, -3000, -3000);
    [_overlayView setNeedsDisplayInMapRect:_overlay.boundingMapRect];
}

The problem appears when MKMapView is zoomed in (right side):

Test Xcode project on bitbucket

来源:https://stackoverflow.com/questions/15525526/mkmapview-not-updating-overlay-correctly-after-changing-its-boundingmaprect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!