草庐IT

ios - MKCircleRenderer 填充颜色重叠问题

coder 2024-01-16 原文

我正在将 MKCircleRenderer 添加到 map 代码中

- (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;

}

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

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

最佳答案

与其创建多个 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

关于ios - MKCircleRenderer 填充颜色重叠问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29204125/

有关ios - MKCircleRenderer 填充颜色重叠问题的更多相关文章

  1. ruby - 在 64 位 Snow Leopard 上使用 rvm、postgres 9.0、ruby 1.9.2-p136 安装 pg gem 时出现问题 - 2

    我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po

  2. ruby - 什么是填充的 Base64 编码字符串以及如何在 ruby​​ 中生成它们? - 2

    我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%

  3. ruby - 通过 rvm 升级 ruby​​gems 的问题 - 2

    尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub

  4. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  5. ruby - 通过 RVM (OSX Mountain Lion) 安装 Ruby 2.0.0-p247 时遇到问题 - 2

    我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search

  6. ruby - Fast-stemmer 安装问题 - 2

    由于fast-stemmer的问题,我很难安装我想要的任何ruby​​gem。我把我得到的错误放在下面。Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingfast-stemmer:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcreatingMakefilemake"DESTDIR="cleanmake"DESTDIR=

  7. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  8. ruby - 匹配大写字母并用后续字母填充,直到一定的字符串长度 - 2

    我有一个驼峰式字符串,例如:JustAString。我想按照以下规则形成长度为4的字符串:抓取所有大写字母;如果超过4个大写字母,只保留前4个;如果少于4个大写字母,则将最后大写字母后的字母大写并添加字母,直到长度变为4。以下是可能发生的3种情况:ThisIsMyString将产生TIMS(大写字母);ThisIsOneVeryLongString将产生TIOV(前4个大写字母);MyString将生成MSTR(大写字母+tr大写)。我设法用这个片段解决了前两种情况:str.scan(/[A-Z]/).first(4).join但是,我不太确定如何最好地修改上面的代码片段以处理最后一种

  9. ruby - 安装 Ruby 时遇到问题(无法下载资源 "readline--patch") - 2

    当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub

  10. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

随机推荐