Subclassing MKOVerlay View- canDrawMapRect not called
I am attempting to subclass MKOverlayView to create a custom overlay. I understand that in order to do this, one must override the following two methods
- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context;
- (BOOL)canDrawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale;
My question arrises from the latter method. For some reason, when I override it in my MKOverlayView subclass,开发者_如何学运维 it does not get called. According to documentation, it should be called before tiles are rendered and if it returns YES, then drawMapRect is called. I was hoping that someone could look at the following code and see if they can figure out why this method is not being called. Is it meant to be enabled/called manually somewhere?
Interestingly enough, drawMapRect does get called, it's only canDrawMapRect that does not. Am I misinterpreting the functionality of canDrawMapRect or is something wrong in my code?
HeatMapOverlay.h
#import <MapKit/MapKit.h>
#import <Foundation/Foundation.h>
@interface HeatMapOverlayView : MKOverlayView{
...variables...
}
@end
HeatMapOverlay.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...
}
Thank you!
Try changing this line:
self = [super init];
to this to use the proper initializer for an MKOverlayView
:
self = [super initWithOverlay:overlay];
精彩评论