重写的 MKMapView drawMapRect 方法返回错误
Overridden MKMapView drawMapRect method returning errors
我覆盖了我的 .m 文件中的 drawMapRect 方法,但似乎重写方法中的某些代码行给我错误,我不确定如何解决。我确保将 mkmapview 委托给自己,所有正确的包都已导入。这是 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];
}
第 2 行代码返回错误,“No visible @interface for 'MapViewClass' declares the selector for 'rectForMapRect:'
最后一行代码返回错误。 “'UIViewController' 没有可见的@interface 声明了选择器 'drawMapRect: zoomScale: inContext:'
编辑
看了妲己的回答后,我对自己的代码做了如下改动。我创建了一个新的 class,它是 MKOverlayRenderer 的子 class,并将 drawMapRect 方法移动到那个 class。我之前收到的以前的错误现在都消失了,但是没有绘制叠加层。这是来自我的 MapViewClass .m 文件以及 MKOverlayRenderer subclass.
的附加代码
#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
地图视图类.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 -- 尽管视图控制器不是地图视图:)
您的 VC 可能有地图视图,但它不是地图视图
2.
但更重要的是:
该代码也不属于 mapView。您尝试使用地图叠加层。考虑使用 MKOverlayRenderer
两条错误消息基本上就是这样说的:
- 说你的 SELF(ViewController) 没有名为
rectForMapRect
的方法
- 说你的 SUPER(UIViewController) 没有名为
drawMapRect
的方法
==> 只有 MKOverlayRenderer 提供它们(如果您搜索这两种方法,可以从文档中看到)
我覆盖了我的 .m 文件中的 drawMapRect 方法,但似乎重写方法中的某些代码行给我错误,我不确定如何解决。我确保将 mkmapview 委托给自己,所有正确的包都已导入。这是 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];
}
第 2 行代码返回错误,“No visible @interface for 'MapViewClass' declares the selector for 'rectForMapRect:'
最后一行代码返回错误。 “'UIViewController' 没有可见的@interface 声明了选择器 'drawMapRect: zoomScale: inContext:'
编辑 看了妲己的回答后,我对自己的代码做了如下改动。我创建了一个新的 class,它是 MKOverlayRenderer 的子 class,并将 drawMapRect 方法移动到那个 class。我之前收到的以前的错误现在都消失了,但是没有绘制叠加层。这是来自我的 MapViewClass .m 文件以及 MKOverlayRenderer subclass.
的附加代码#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
地图视图类.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 -- 尽管视图控制器不是地图视图:)
您的 VC 可能有地图视图,但它不是地图视图
2.
但更重要的是:
该代码也不属于 mapView。您尝试使用地图叠加层。考虑使用 MKOverlayRenderer
两条错误消息基本上就是这样说的:
- 说你的 SELF(ViewController) 没有名为
rectForMapRect
的方法
- 说你的 SUPER(UIViewController) 没有名为
drawMapRect
的方法
==> 只有 MKOverlayRenderer 提供它们(如果您搜索这两种方法,可以从文档中看到)