【问题标题】:MKCircleRenderer fillcolour overlap issueMKCircleRenderer 填充颜色重叠问题
【发布时间】:2015-03-23 06:06:08
【问题描述】:

我将 MKCircleRenderer 添加到地图代码中

- (MKOverlayRenderer*)mapView:(MKMapView*)mapView rendererForOverlay:(id <MKOverlay>)overlay
{
     MKCircle* circle = overlay;
        MKCircleRenderer *circleView = [[MKCircleRenderer alloc] initWithOverlay:circle];
       circleView.fillColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:0.1];
      return circleView;

}

这里的问题是,当两个圆圈重叠时,我不希望它们“混合”并在重叠区域显示较暗的颜色,就像这样。

任何人都可以提供任何提示/解决方案来解决这个问题。

【问题讨论】:

  • 请注意,这似乎不是实际代码,因为照片显示绿色圆圈,代码显示红色圆圈 :)

标签: ios iphone mkmapview mkoverlay


【解决方案1】:

与其创建多个 MKCircle/MKCircleRenderer 叠加层,不如创建一个使用 MKOverlayPathRenderer 的叠加层,其中包含由单个圆圈的联合组成的路径。然后用适当的颜色填充该路径。

这里有相当多的代码,但它似乎可以满足您的需求

//
//  CirclesOverlay
//  Circles
//
//  Created by David W. Berry on 3/26/15.
//  Copyright (c) 2015 Greenwing Software. All rights reserved.
//

#import <UIKit/UIKit.h>

@import MapKit;

@interface Circle : NSObject

@property (nonatomic, assign) CLLocationCoordinate2D    center;
@property (nonatomic, assign) CGFloat                   width;
@property (nonatomic, assign) CGFloat                   height;

+(Circle*)withCenter:(CLLocationCoordinate2D)center width:(CGFloat)width height:(CGFloat)height;
-(id)initWithCenter:(CLLocationCoordinate2D)center width:(CGFloat)width height:(CGFloat)height;

@end


@interface CirclesOverlay : NSObject<MKOverlay>

@property (nonatomic, strong, readonly) NSArray*        circles;
@property (nonatomic, strong, readonly) UIColor*        color;
@property (nonatomic, readonly) CLLocationCoordinate2D  coordinate;
@property (nonatomic, readonly) MKMapRect               boundingMapRect;

+(CirclesOverlay*)withCircles:(NSArray*)circles color:(UIColor*)color;
-(id)initWithCircles:(NSArray*)circles color:(UIColor*)color;

@end

@interface CirclesOverlayRenderer : MKOverlayPathRenderer

+(CirclesOverlayRenderer*)withCirclesOverlay:(CirclesOverlay*)circlesOverlay;
-(id)initWithCirclesOverlay:(CirclesOverlay*)circlesOverlay;

@end


//
//  CirclesOverlay.m
//  Circles
//
//  Created by David W. Berry on 3/26/15.
//  Copyright (c) 2015 Greenwing Software. All rights reserved.
//

#import "CirclesOverlay.h"

// This should probably be somewhere other than in the ViewController, but for an example it's fine
static MKMapRect MKMapRectForCoordinateRegion(MKCoordinateRegion region)
{
    MKMapPoint a = MKMapPointForCoordinate(CLLocationCoordinate2DMake(
                                                                      region.center.latitude + region.span.latitudeDelta / 2,
                                                                      region.center.longitude - region.span.longitudeDelta / 2
                                                                      )
                                           );
    MKMapPoint b = MKMapPointForCoordinate(CLLocationCoordinate2DMake(
                                                                      region.center.latitude - region.span.latitudeDelta / 2,
                                                                      region.center.longitude + region.span.longitudeDelta / 2
                                                                      )
                                           );

    return MKMapRectMake(MIN(a.x,b.x), MIN(a.y,b.y), ABS(a.x-b.x), ABS(a.y-b.y));
}

@implementation Circle

-(MKMapRect)mapRect
{
    return MKMapRectForCoordinateRegion(
                                        MKCoordinateRegionMakeWithDistance(self.center, self.height, self.width)
                                        );
}

+(Circle*)withCenter:(CLLocationCoordinate2D)center width:(CGFloat)width height:(CGFloat)height
{
    return [[self alloc] initWithCenter:center width:width height:height];
}

-(id)initWithCenter:(CLLocationCoordinate2D)center width:(CGFloat)width height:(CGFloat)height
{
    if(self = [super init])
    {
        self.center = center;
        self.width = width;
        self.height = height;
    }
    return self;
}

@end

@interface CirclesOverlay ()

@property (nonatomic, strong) NSArray*        circles;
@property (nonatomic, strong) UIColor*        color;

@end

@implementation CirclesOverlay

-(CLLocationCoordinate2D)coordinate
{
    MKMapRect   bounds = self.boundingMapRect;

    return MKCoordinateForMapPoint(MKMapPointMake(
                                                  bounds.origin.x + bounds.size.width / 2.0,
                                                  bounds.origin.y + bounds.size.height / 2.0
                                                  ));
}

-(MKMapRect)boundingMapRect
{
    MKMapRect   bounds = MKMapRectNull;

    for(Circle* circle in self.circles)
    {
        bounds = MKMapRectUnion(bounds, circle.mapRect);
    }

    return bounds;
}

+(CirclesOverlay*)withCircles:(NSArray*)circles color:(UIColor*)color
{
    return [[self alloc] initWithCircles:circles color:color];
}

-(id)initWithCircles:(NSArray*)circles color:(UIColor*)color
{
    if(self = [super init])
    {
        self.circles = circles;
        self.color = color ?: [[UIColor redColor] colorWithAlphaComponent:0.10];
    }
    return self;
}

@end

@implementation CirclesOverlayRenderer

-(CirclesOverlay*)circlesOverlay
{
    return (CirclesOverlay*)self.overlay;
}

+(CirclesOverlayRenderer*)withCirclesOverlay:(CirclesOverlay*)circlesOverlay
{
    return [[self alloc] initWithCirclesOverlay:circlesOverlay];
}

-(id)initWithCirclesOverlay:(CirclesOverlay*)circlesOverlay
{
    if((self = [super initWithOverlay:circlesOverlay]))
    {
        self.fillColor = circlesOverlay.color;
    }
    return self;
}

-(void)createPath
{
    CGMutablePathRef path = CGPathCreateMutable();

    for(Circle* circle in self.circlesOverlay.circles)
    {
        MKMapRect   mapRect = circle.mapRect;
        CGRect      cgRect = [self rectForMapRect:mapRect];

        CGPathAddEllipseInRect(path, NULL, cgRect);
    }

    self.path = path;
}

@end



//
//  ViewController.m
//  Circles
//
//  Created by David W. Berry on 3/26/15.
//  Copyright (c) 2015 Greenwing Software. All rights reserved.
//

#import "ViewController.h"
#import "CirclesOverlay.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.mapView.region = MKCoordinateRegionMake(
                                                 CLLocationCoordinate2DMake(37.3347606, -122.054883),
                                                 MKCoordinateSpanMake(0.0725, 0.0725)
                                                 );

    CirclesOverlay* overlay = [CirclesOverlay withCircles:@[
                                                            [Circle withCenter:CLLocationCoordinate2DMake(37.3347606, -122.054883) width:1000.0 height:1000.0],
                                                            [Circle withCenter:CLLocationCoordinate2DMake(37.3477606, -122.055883) width:1000.0 height:1000.0],
                                                            [Circle withCenter:CLLocationCoordinate2DMake(37.3397606, -122.054883) width:1000.0 height:1000.0],
                                                            ]
                                                    color:nil];

    [self.mapView addOverlay:overlay];
}

-(MKOverlayRenderer*)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
    if([overlay isKindOfClass:[CirclesOverlay class]])
    {
        return [CirclesOverlayRenderer withCirclesOverlay:(CirclesOverlay*)overlay];
    }
    else
    {
        return nil;
    }
}

@end

【讨论】:

  • 谢谢@David Berry,看来你的答案会得到锻炼,你有没有任何与个人圈子相关的示例代码/链接联合对我更有帮助。
  • 非常感谢大卫,但是如果我使用 CGContextSetRGBStrokeColor(context, 0, 0, 0, 0.75f); CGContextSetLineWidth(context, 1.0f / zoomScale); CGContextStrokeEllipseInRect(context, cgRect);与填充颜色一起,它与其他圆圈重叠。有没有办法将笔触颜色与填充颜色结合起来。
  • 你好大卫,self.strokeColor = [[UIColor grayColor] colorWithAlphaComponent:0.5];我试着用上面的线画笔画,笔画不像圆圈,像这张照片一样,笔画相互重叠。 s7.postimg.org/u3w8i0sfv/… 是否有任何替代来获得边界笔划。
  • 我不知道。您本质上是在要求路径的联合,并且没有简单的方法来获得它。这里有一些关于解决方案的讨论。 groups.google.com/forum/m/#!topic/brighton-iphone-creators/…
猜你喜欢
  • 1970-01-01
  • 2021-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-19
  • 1970-01-01
  • 2017-03-03
相关资源
最近更新 更多