MKOverlayViews と MKMapKit に関する多くの問題に取り組んだ結果、もう 1 つ問題が発生しました。MKOverlayPathView サブクラスを使用して、画面上に特異な大きなオーバーレイを描画しています (現時点ではロンドンをカバーしています)。そのオーバーレイ内には、議論のために色付きの四角形がたくさんあります。
- (void)drawMapRect:(MKMapRect)mapRect
zoomScale:(MKZoomScale)zoomScale
inContext:(CGContextRef)context
{
int size = 0;
min = 0;
max = 0;
CGFloat alpha = 0.4f;
int recursionLevel = 4;
QuadNodeOverlay *quadNodeOverlay = (QuadNodeOverlay *)self.overlay;
Cluster *clusters = quadtree_clusters(quadNodeOverlay.quadTree, recursionLevel, &size);
if (clusteringMethod == 1) {
/* Draw paths/squares */
CGPathRef *paths = [self createPaths:clusters size:size];
for (int i = 0; i < size; i++) {
CGPathRef path = paths[i];
Cluster cluster = clusters[i];
CGContextBeginPath(context);
CGContextAddPath(context, path);
//CGContextSetFillColorWithColor(context, colors[cluster.depth]);
CGColorRef gradientColor = [self newColor:cluster.count];
CGContextSetFillColorWithColor(context, gradientColor);
CGContextDrawPath(context, kCGPathFillStroke);
CGColorRelease(gradientColor);
CGPathRelease(path);
}
free(paths);
} else if (clusteringMethod == 2) {
// CUT
}
[self setAlpha:alpha];
}
上記のコードは不完全ですが、質問の確固たる基礎を提供します。コードは正常に動作し、描画する正方形の数に関係なく、驚くほど高速に実行されます。私が抱えている問題は、このオーバーレイを描画していない特定のポイントの下にあります。マップを含むView Controllerでこれを管理します:
- (void)mapView:(MKMapView *)map regionDidChangeAnimated:(BOOL)animated {
self.annotations = [locationServer itemsForMapRegion:map.region withMapView:map maxCount:100];
NSLog(@"There are like %d cards dude.",[self.annotations count]);
if ( [self.annotations count] > 40) {
[mapView removeAnnotations:annotations];
// Cluster
if (![[mapView overlays] containsObject:quadClusters]) {
[mapView addOverlay:quadClusters];
}
} else {
// Don't
[mapView removeOverlay:quadClusters];
// Add pins.
[mapView removeAnnotations:annotations];
[mapView addAnnotations:annotations];
}
}
繰り返しますが、このコードは現在私の目的に適しています。問題は、クラスターが表示されないレベルにズームインすると、個々の注釈がズームアウトしてオーバーレイのチャックが透明度なしでレンダリングされることです。表示:
脚注として。オーバーレイから setAlpha を削除し、正方形を描画して読み取るコードを変更します。
CGColorRef gradientColor = [self newColor:cluster.count];
CGColorRef alphaGradientColor = CGColorCreateCopyWithAlpha(gradientColor, alpha);
CGContextSetFillColorWithColor(context, alphaGradientColor);
問題は解決しますが、オーバーレイ全体にアルファ チャネルを適用する方が確実に効率的です。