首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何呈现复合MKOverlay

如何呈现复合MKOverlay
EN

Stack Overflow用户
提问于 2014-09-03 18:50:24
回答 1查看 555关注 0票数 1

我想要创建一个MKOverlay,它可以将几个MKShapes组合在一起,但是组合的MKOverlayRenderer出现了问题。

我想避免将所有这些作为单独的MKOverlays和MKOverlayRenderers来处理,因为( a)这是很多文件,而b)它们在概念上是相同的。

这是我所拥有的。来自MyOverlay.h:

代码语言:javascript
复制
@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:

代码语言:javascript
复制
- (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:

代码语言:javascript
复制
@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则应沿圆的边缘。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-05 07:39:11

是的,问题出在drawMapRect:zoomScale:inContext:。在绘制合成元素之前,我必须将其转换为每个元素的来源。这样做是可行的:

代码语言:javascript
复制
- (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);
}

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25651617

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档