ios - 重写的 MKMapView drawMapRect 方法返回错误

标签 ios objective-c xcode mkmapview mkoverlay

我重写了 .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


这就是两条错误消息的基本含义:

  1. 说你的 SELF(ViewController) 没有名为 rectForMapRect 的方法
  2. 说你的 SUPER(UIViewController) 没有名为 drawMapRect 的方法

==> 只有 MKOverlayRenderer 提供它们(如果您搜索这两种方法,可以从文档中看到)

关于ios - 重写的 MKMapView drawMapRect 方法返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29186396/

相关文章:

ios - Realm 无法在 Xcode 9.2 上使用 SIGNAL SIGABRT

ios - 苹果分发证书导致 14094410 :SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure

ios - 为什么使用开发者身份或第三方通过后端进行身份验证

ios - 如果我以编程方式创建 View 并将它们添加到 View 层次结构中,为什么它们需要成为强引用?

ios - uiimagePickerController - 选择媒体后不会调用 didFinishPickingMediaWithInfo

ios - Firebase 匿名登录 - 为什么 FirebaseID 在登录到 Facebook 时发生变化,但在未登录到 Facebook 时保持不变?

objective-c - 我应该把 removeObserver 放在 AppDelegate.m 的什么地方,ios

iphone - Facebook SDK 3.0 授权后启动 View Controller

在 Windows 的 Xcode 中编译(控制台)

ios - 创建应用程序时是否必须指定 "use core data "?