问题描述
我正在尝试将MKOverlayView子类化以创建自定义叠加层.我了解,要做到这一点,必须重写以下两种方法
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;
我的问题源于后一种方法.由于某些原因,当我在MKOverlayView子类中重写它时,不会调用它.根据文档,应在渲染图块之前调用它,如果返回YES,则将调用drawMapRect.我希望有人可以看下面的代码,看看他们是否能弄清楚为什么不调用此方法.是要在某个地方手动启用/调用吗?
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?
有趣的是,调用了drawMapRect ,只有canDrawMapRect不会.我是在误解canDrawMapRect的功能还是我的代码有问题?
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
HeatMapOverlay.h
#import <MapKit/MapKit.h>
#import <Foundation/Foundation.h>
@interface HeatMapOverlayView : MKOverlayView{
...variables...
}
@end
HeatMapOverlay.m
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...
}
谢谢!
推荐答案
尝试更改此行:
self = [super init];
为此,将正确的初始化程序用于MKOverlayView
:
to this to use the proper initializer for an MKOverlayView
:
self = [super initWithOverlay:overlay];
这篇关于子类化MKOVerlay View- canDrawMapRect不被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!