ios - 如何渲染复合 MKOverlay

标签 ios mkmapview mapkit drawrect mkoverlay

我想创建一个将几个 MKShape 组合在一起的 MKOverlay,但我在使用复合 MKOverlayRenderer 时遇到了问题。

我想避免将所有这些都作为单独的 MKOverlays 和 MKOverlayRenderer 来完成,因为 a) 这是很多文件并且 b) 它们在概念上是相同的。

这是我的。来自 MyOverlay.h:

@property (nonatomic) CLLocationCoordinate2D coordinate;
@property (nonatomic, readonly) MKMapRect boundingMapRect;
@property (strong, nonatomic, readonly) MKCircle *circle;
@property (strong, nonatomic, readonly) MKCircle *editCircle;
@property (strong, nonatomic, readonly) MKPolyline *radiusLine;

来自 MyOverlay.m:

- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate {
    self = [super init];
    if (self) {
        _coordinate = coordinate;
        _circle = [MKCircle circleWithCenterCoordinate:_coordinate radius:_radius];
        _editCircle = [MKCircle circleWithCenterCoordinate:_coordinate radius:_radius * 0.1];
        CLLocationCoordinate2D offset = [self translateCoord:_coordinate distanceLat:0.0 distanceLong:_radius];
        CLLocationCoordinate2D coords[2];
        coords[0] = _coordinate;
        coords[1] = offset;
        _radiusLine = [MKPolyline polylineWithCoordinates:coords count:2];
        _boundingMapRect = MKMapRectUnion(MKMapRectUnion(_circle.boundingMapRect, _editCircle.boundingMapRect), _radiusLine.boundingMapRect);
    }
    return self;
}

来自 MyOverlayRenderer.m:

@interface MyOverlayRenderer ()

@property (nonatomic, strong) MKCircleRenderer *circleRenderer;
@property (nonatomic, strong) MKCircleRenderer *editCircleRenderer;
@property (nonatomic, strong) MKPolylineRenderer *radiusLineRenderer;
@property (nonatomic) MKMapRect circleBoundingMapRect;
@property (nonatomic) MKMapRect editCircleBoundingMapRect;
@property (nonatomic) MKMapRect radiusLineBoundingMapRect;

@end

@implementation MyOverlayRenderer

- (instancetype)initWithMyOverlay:(MyOverlay *)overlay {
    self = [super initWithOverlay: overlay];
    if (self) {
        _circleRenderer = [[MKCircleRenderer alloc] initWithCircle: overlay.circle];
        _circleRenderer.lineWidth = 2.0;
        _circleRenderer.strokeColor = overlay.color;
        _circleBoundingMapRect = overlay.circle.boundingMapRect;
        CGFloat red, green, blue, alpha;
        [overlay.color getRed:&red green:&green blue:&blue alpha:&alpha];
        _circleRenderer.fillColor = [UIColor colorWithRed:red green:green blue:blue alpha:alpha * 0.2];
        _editCircleRenderer = [[MKCircleRenderer alloc] initWithCircle:overlay.editCircle];
        _editCircleRenderer.fillColor = overlay.color;
        _editCircleBoundingMapRect = overlay.editCircle.boundingMapRect;
        _radiusLineRenderer = [[MKPolylineRenderer alloc] initWithPolyline:overlay.radiusLine];
        _radiusLineRenderer.lineWidth = 1.5;
        _radiusLineRenderer.strokeColor = overlay.color;
        _radiusLineRenderer.lineDashPattern = @[@2.0, @2.0];
        _radiusLineBoundingMapRect = overlay.radiusLine.boundingMapRect;
    }
    return self;
}

- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context {
    double x = MKMapRectGetMidX(mapRect);
    double y = MKMapRectGetMidY(mapRect);
    CGContextSaveGState(context);
    [_circleRenderer drawMapRect:_circleBoundingMapRect zoomScale:zoomScale inContext:context];
    CGContextRestoreGState(context);
    CGContextSaveGState(context);
    CGContextMoveToPoint(context, x, y);
    [_editCircleRenderer drawMapRect:_editCircleBoundingMapRect zoomScale:zoomScale inContext:context];
    CGContextRestoreGState(context);
    CGContextMoveToPoint(context, x, y);
    [_radiusLineRenderer drawMapRect:_radiusLineBoundingMapRect zoomScale:zoomScale inContext:context];
}

结果很难看。 radiusLine 应该以圆心为起点,editCircle 应该沿着圆的边缘。

myOverlay Results

我相信我的错误是在 drawMapRect:zoomScale:inContext: 方法中——这只是我在那里尝试的最新代码——但我不确定错误是什么....

最佳答案

是的,问题出在 drawMapRect:zoomScale:inContext: 中。在绘制它们之前,我必须转换为合成中每个元素的来源。这有效:

- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context {
    CGPoint p;

    CGContextSaveGState(context);
    p = [self pointForMapPoint:_circleMapRect.origin];
    CGContextTranslateCTM(context, p.x, p.y);
    [_circleRenderer drawMapRect:_circleBoundingMapRect zoomScale:zoomScale inContext:context];

    CGContextRestoreGState(context);
    CGContextSaveGState(context);
    p = [self pointForMapPoint:_editCircleBoundingMapRect.origin];
    CGContextTranslateCTM(context, p.x, p.y);
    [_editCircleRenderer drawMapRect:_editCircleBoundingMapRect zoomScale:zoomScale inContext:context];

    CGContextRestoreGState(context);
    CGContextSaveGState(context);
    p = [self pointForMapPoint:_radiusLineBoundingMapRect.origin];
    CGContextTranslateCTM(context, p.x, p.y);
    [_radiusLineRenderer drawMapRect:_radiusLineBoundingMapRect zoomScale:zoomScale inContext:context];
    CGContextRestoreGState(context);
}

Much better.

关于ios - 如何渲染复合 MKOverlay,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25651617/

相关文章:

ios - 如何添加跨越两个 View 的渐变?

ios - 可以关闭 MKMapView 中的动态文本吗?

iphone - 将图像添加到 MapView 注释

ios - 使用 mapKit 的 map 显示相反的方向 (iOS Swift)

iOS 魔法记录从数组导入

ios - AlamofireImage 下载到 CoreData

ios - 当我在社交媒体中通过 Gigya 集成和登录时,应用程序崩溃

iphone - 强制 MapKit 仅使用缓存的 map 图 block /以编程方式禁用网络

ios - 固定 MKAnnotation 大小

ios - 快速删除多段线