gpt4 book ai didi

objective-c - MKMapView 与一个 MKOverlayPathView 会导致 map 模糊

转载 作者:行者123 更新时间:2023-11-29 04:48:00 25 4
gpt4 key购买 nike

任何人都可以回答以下两个问题中的任何一个

1) 如果我根据 MKOverlayPathView 中的 ZoomScale 动态调整覆盖对象的大小,当不知道 ZoomScale 是什么时,如何确保 MKOverlay 协议(protocol)支持类返回正确的boundingMapRect?

2)boundingMapRect太小有什么副作用?

原创....

我是一个 MKMapView 和一个 MKOverlayPathView。将叠加层添加到 map 后,平移和缩放会使底层 map 的部分变得模糊,或者在某些情况下无法下载。叠加 View 本身很好并且清晰, map 数据变得模糊(从之前的缩放级别来看)。

如果未添加叠加层,则一切正常。

MKOverlayPathView 是子类,并且具有点和大小的数组,它使用这些点和大小将实心圆绘制到单个叠加层中以进行显示。边界矩形由所有点的并集形成,这些点占它们将产生的圆的半径。

那我错过了什么?是绘制过程、boundingRect还是其他什么?

示例代码:

MapViewController代码

- (MKOverlayView *)mapView:(MKMapView *)map viewForOverlay:(id <MKOverlay>)overlay
{
if ([overlay isKindOfClass:[MultiCluster class]]) {
MKMultiClusterView *multiclusterView = [[MKMultiClusterView alloc] initWithOverlay:overlay];
return multiclusterView;
}

return nil;
}

MultiCluster.h

@interface MultiCluster : NSObject <MKOverlay> {
NSArray *_clusters;
MKMapRect _boundingMapRect;
}

- (id)initWithClusters:(NSArray *)clusters;

@property (nonatomic, strong) NSArray *clusters;

@end

MultiCluster.m

@implementation MultiCluster

@synthesize clusters = _clusters;

- (id)initWithClusters:(NSArray *)clusters {
if (self = [super init]) {
_clusters = [clusters copy];

NSUInteger clusterCount = [_clusters count];
if (clusterCount) {
_boundingMapRect = [[_clusters objectAtIndex:0] boundingMapRect];
NSUInteger i;
for (i = 1; i < clusterCount; i++) {
_boundingMapRect = MKMapRectUnion(_boundingMapRect, [[_clusters objectAtIndex:i] boundingMapRect]);
}
}
}

return self;
}

- (void)setClusters:(NSArray *)clusters {
_clusters = [clusters copy];
NSUInteger clusterCount = [_clusters count];
if (clusterCount) {
_boundingMapRect = [[_clusters objectAtIndex:0] boundingMapRect];
NSUInteger i;
for (i = 1; i < clusterCount; i++) {
_boundingMapRect = MKMapRectUnion(_boundingMapRect, [[_clusters objectAtIndex:i] boundingMapRect]);
}
}
}

- (MKMapRect)boundingMapRect {
return _boundingMapRect;
}

- (CLLocationCoordinate2D)coordinate {
return MKCoordinateForMapPoint(MKMapPointMake(MKMapRectGetMidX(_boundingMapRect), MKMapRectGetMidY(_boundingMapRect)));
}

@end

MKMultiClusterView.h

@interface MKMultiClusterView : MKOverlayPathView {
CGColorRef *colors;
}

@end

MKMultiClusterView.m

@implementation MKMultiClusterView

// Create a table of possible colors to draw a grid cell with
- (void)initColors
{
CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
colors = malloc(sizeof(CGColorRef) * NUM_COLORS);
int i = 0;
colors[i++] = CGColorCreate(rgb, (CGFloat[]){ .588, .294, .78, OA }); // 1.00
colors[i++] = CGColorCreate(rgb, (CGFloat[]){ .784, .471, .82, OA }); // 0.77
colors[i++] = CGColorCreate(rgb, (CGFloat[]){ 1, 0, 0, OA }); // 0.59
colors[i++] = CGColorCreate(rgb, (CGFloat[]){ 1, .392, 0, OA }); // 0.46
colors[i++] = CGColorCreate(rgb, (CGFloat[]){ 1, .392, 0, OA }); // 0.35
colors[i++] = CGColorCreate(rgb, (CGFloat[]){ 1, .784, 0, OA }); // 0.27
colors[i++] = CGColorCreate(rgb, (CGFloat[]){ 1, 1, .5, OA }); // 0.21
colors[i++] = CGColorCreate(rgb, (CGFloat[]){ .745, .941, .467, OA }); // 0.16
colors[i++] = CGColorCreate(rgb, (CGFloat[]){ .122, 1, .31, OA }); // 0.12
colors[i++] = CGColorCreate(rgb, (CGFloat[]){ .588, 1, .941, OA }); // 0.10
colors[i++] = CGColorCreate(rgb, (CGFloat[]){ .784, 1, 1, OA }); // 0.08
colors[i++] = CGColorCreate(rgb, (CGFloat[]){ .843, 1, 1, OA }); // 0.06
colors[i++] = CGColorCreate(rgb, (CGFloat[]){ .902, 1, 1, OA }); // 0.04
colors[i] = CGColorCreate(rgb, (CGFloat[]){ .784, .784, .784, OA }); // 0.03
CGColorSpaceRelease(rgb);
}

// Look up a color in the table of colors for a peak ground acceleration
- (CGColorRef)colorForSize:(int)value
{
if (value > 3000) return colors[0];
if (value > 2500) return colors[1];
if (value > 2000) return colors[2];
if (value > 1500) return colors[3];
if (value > 1000) return colors[4];
if (value > 540) return colors[5];
if (value > 480) return colors[6];
if (value > 420) return colors[7];
if (value > 360) return colors[8];
if (value > 300) return colors[9];
if (value > 240) return colors[10];
if (value > 180) return colors[11];
if (value > 120) return colors[12];
if (value <= 120) return colors[13];
return NULL;
}

- (id)initWithOverlay:(id<MKOverlay>)overlay {
if (self = [super initWithOverlay:overlay])
{
[self initColors];
}
return self;
}

- (CGPathRef)createPath:(Cluster *)cluster zoomScale:(MKZoomScale)zoomScale {
CGMutablePathRef path = CGPathCreateMutable();
// Calculate radius for circle in map pixels.
double fudge = 2;
double viewWidth = 320;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
fudge = 5;
viewWidth = 768;
}
// Radius of circle in pixels.
int radiusPixels = 10 + (fudge * log([cluster.members count]));
fudge = 1/zoomScale;
double radiusPoints = radiusPixels * fudge;
double diameterPoints = radiusPoints * 2;

// Center of circle in map points.
CGPoint relativePoint = [self pointForMapPoint:MKMapPointForCoordinate(cluster.coordinate)];
CGRect ellipseRect = CGRectMake(relativePoint.x - radiusPoints,
relativePoint.y - radiusPoints,
10000,
10000);
CGPathAddEllipseInRect(path, NULL, ellipseRect);

return path;
}

- (void)drawMapRect:(MKMapRect)mapRect
zoomScale:(MKZoomScale)zoomScale
inContext:(CGContextRef)context
{
MultiCluster *multiCluster = (MultiCluster *)self.overlay;
for (Cluster *cluster in multiCluster.clusters) {
CGPathRef path = [self createPath:cluster zoomScale:zoomScale];
if (path) {
self.fillColor = [UIColor colorWithCGColor:[self colorForSize:[cluster.members count]]];
[self applyFillPropertiesToContext:context atZoomScale:zoomScale];
CGContextBeginPath(context);
CGContextAddPath(context, path);
CGContextDrawPath(context, kCGPathEOFill);
CGPathRelease(path);
}
}
}

@end

MKMultiClusterView 中有一些测试代码,例如未使用的变量和固定而不是动态的圆大小。它与问题无关(无论圆的大小是固定的还是可变的,问题仍然会出现)。

如果您想要包含更多代码,请发表评论,我会找到某个地方将其作为示例项目上传或包含此问题中的相关部分。

相同代码的完全静态实现不会出现问题,它会通过引入 alpha 进行修正。那么我可以有把握地说这是boundingMapRect 的问题吗?

最佳答案

您是否尝试过将 alpha 分量从各个圆圈移动到图层本身?

关于objective-c - MKMapView 与一个 MKOverlayPathView 会导致 map 模糊,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9328239/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com