我重写了 .m 文件中的 drawMapRect 方法,但重写方法中的某些代码行似乎给了我错误,我不确定如何解决。我确保将 mkmapview 委托(delegate)设置为 self,导入了所有正确的包。这是drawMapRect方法的代码,
-(void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context{
CGRect rect = [self rectForMapRect:mapRect];
CGContextSaveGState(context);
CGContextAddRect(context, rect);
CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
CGContextFillRect(context, rect);
CGContextRestoreGState(context);
[super drawMapRect:mapRect zoomScale: zoomScale inContext:context];
}
第二行代码返回错误,“'MapViewClass' 没有可见的 @interface 声明了 'rectForMapRect:' 的选择器
最后一行代码返回错误。 “‘UIViewController’没有可见的@interface声明选择器‘drawMapRect:zoomScale:inContext:’
编辑 看了大吉的回答后,我对我的代码做了以下修改。我创建了一个新类,它是 MKOverlayRenderer 的子类,并将 drawMapRect 方法移至该类。我之前收到的错误现在消失了,但叠加层没有被绘制。以下是来 self 的 MapViewClass .m 文件以及 MKOverlayRenderer 子类的附加代码。
#import "MapOverlayRenderer.h"
@implementation MapOverlayRenderer
-(void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context{
CGRect rect = [self rectForMapRect:mapRect];
CGContextSaveGState(context);
CGContextAddRect(context, rect);
CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
CGContextFillRect(context, rect);
CGContextRestoreGState(context);
[super drawMapRect:mapRect zoomScale: zoomScale inContext:context];
}
@end
MapViewClass.m
- (void)viewDidLoad {
mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 75, 320, 493)];
mapView.delegate = self;
mapView.mapType = MKMapTypeStandard;
[self.view addSubview: mapView];
MKMapRect worldRect = MKMapRectWorld;
MKCoordinateRegion worldRegion = MKCoordinateRegionForMapRect(worldRect);
CLLocationCoordinate2D worldPoints[4];
//Creates corners of the whole map
worldPoints[0] = CLLocationCoordinate2DMake(worldRegion.center.latitude + worldRegion.span.latitudeDelta/2.0, worldRegion.center.longitude - worldRegion.span.longitudeDelta/2.0);
worldPoints[1]= CLLocationCoordinate2DMake(worldRegion.center.latitude - worldRegion.span.latitudeDelta/2.0, worldRegion.center.longitude - worldRegion.span.longitudeDelta/2.0);
worldPoints[2] = CLLocationCoordinate2DMake(worldRegion.center.latitude + worldRegion.span.latitudeDelta/2.0, worldRegion.center.longitude + worldRegion.span.longitudeDelta/2.0);
worldPoints[3]= CLLocationCoordinate2DMake(worldRegion.center.latitude - worldRegion.span.latitudeDelta/2.0, worldRegion.center.longitude + worldRegion.span.longitudeDelta/2.0);
MKPolygon *worldSquare = [MKPolygon polygonWithCoordinates:worldPoints count:4];
[mapView addOverlay:worldSquare];
}
-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay{
if([overlay isKindOfClass:[MKPolygon class]]){
MapOverlayRenderer *polygonView = [[MapOverlayRenderer alloc]initWithOverlay:overlay];
return polygonView;
}
}
最佳答案
1.
您将代码放入您的 ViewController——尽管 View Controller 不是 map View :)
您的 VC 可能有 map View ,但它不是 map View
2.
但更重要的是:
代码也不属于 mapView。您尝试使用 map 叠加层。考虑使用 MKOverlayRenderer
这就是两条错误消息的基本含义:
- 说你的 SELF(ViewController) 没有名为
rectForMapRect
的方法 - 说你的 SUPER(UIViewController) 没有名为
drawMapRect
的方法
==> 只有 MKOverlayRenderer 提供它们(如果您搜索这两种方法,可以从文档中看到)
关于ios - 重写的 MKMapView drawMapRect 方法返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29186396/