任何人都可以就以下问题回答这两个问题中的任何一个
1) 如果我根据 MKOverlayPathView 中的 zoomScale 动态调整叠加对象的大小,我如何确保 MKOverlay 协议支持类在不知道 zoomScale 是什么时返回正确的 boundingMapRect?
2) boundingMapRect 太小的副作用是什么?
原来的 ....
我是一个带有一个 MKOverlayPathView 的 MKMapView。将叠加层添加到地图平移和缩放后,底层地图的部分会变得模糊,或者在某些情况下无法下载。覆盖视图本身很好并且焦点集中,它是模糊的地图数据(来自以前的缩放级别)。
如果没有添加叠加层,一切正常。
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;
}
多集群.h
@interface MultiCluster : NSObject <MKOverlay> {
NSArray *_clusters;
MKMapRect _boundingMapRect;
}
- (id)initWithClusters:(NSArray *)clusters;
@property (nonatomic, strong) NSArray *clusters;
@end
多集群.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 上吗?