1

我正在尝试将 MKOverlayView 子类化以创建自定义叠加层。我知道为了做到这一点,必须覆盖以下两种方法

- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context;

- (BOOL)canDrawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale;

我的问题来自后一种方法。出于某种原因,当我在 MKOverlayView 子类中覆盖它时,它不会被调用。根据文档,应该在渲染图块之前调用它,如果它返回 YES,则调用 drawMapRect。我希望有人可以查看以下代码,看看他们是否可以弄清楚为什么没有调用此方法。它是否意味着在某处手动启用/调用?

有趣的是,drawMapRect确实被调用了,只有 canDrawMapRect 没有。我是误解了 canDrawMapRect 的功能还是我的代码有问题?

热图覆盖.h

#import <MapKit/MapKit.h>
#import <Foundation/Foundation.h>
@interface HeatMapOverlayView : MKOverlayView{
    ...variables...
}

@end

热图覆盖.m

#import "HeatMapOverlayView.h"
#import <CoreGraphics/CoreGraphics.h>
#import <QuartzCore/QuartzCore.h>

@implementation HeatMapOverlayView
@synthesize points, heat, QualityIndex;
- (id)initWithOverlay:(id<MKOverlay>)overlay {
    self = [super init];
    if (self) {
        // Initialization code here.
    }
    return self;
}


- (BOOL)canDrawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale{
         ...complete check...
return NO;
}


- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext (CGContextRef)context{
...draw overlay...
}

谢谢!

4

1 回答 1

2

尝试更改此行:

self = [super init];

为此使用正确的初始化程序MKOverlayView

self = [super initWithOverlay:overlay];
于 2011-08-06T02:46:37.920 回答