将MKCircleOverlay添加到MKMapView时无法识别的选择器[MKCircle pointCount]

问题描述 投票:0回答:2

我正在使用Objective C开发适用于iOS 9.0及更高版本的应用程序。

该应用程序包括一个带有MKMapView对象的视图控制器。视图控制器是地图视图委托。

我在viewWillAppear:animated的实现中添加MKCircle叠加层时遇到问题。

使用addOverlay:level:方法无法解决问题。

实现如下:

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.mapView setDelegate:self];
    [self.mapView setZoomEnabled:YES];
    [self.mapView setScrollEnabled:YES];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.locationCircle = [MKCircle circleWithCenterCoordinate:self.locationPin.coordinate radius:1000000.0];
    [self.mapView addOverlay:self.locationCircle];
}

- (MKOverlayRenderer *)rendererForOverlay:(id<MKOverlay>)overlay {
    if ([overlay isKindOfClass:[MKCircle class]]) {
        MKCircleRenderer *renderer = [[MKCircleRenderer alloc] initWithCircle:(MKCircle *)overlay];
        renderer.fillColor = [[UIColor yellowColor] colorWithAlphaComponent:0.25];
        return renderer;
    } else {
        return [self.superclass rendererForOverlay:overlay];
    }
}

具体来说,只要地图视图尝试显示圆形叠加层的任何部分,应用就会崩溃。

该应用程序在地图视图上成功渲染多边形叠加和各种引脚注释。错误消息详细信息为:

2016-11-07 12:35:21.643 [MKCircle pointCount]: unrecognized selector sent to instance 0x6000004902c0
2016-11-07 12:35:21.645 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MKCircle pointCount]: unrecognized selector sent to instance 0x6000004902c0'
*** First throw call stack:
(
0   CoreFoundation                      0x000000010cdc334b __exceptionPreprocess + 171
1   libobjc.A.dylib                     0x000000010c82421e objc_exception_throw + 48
2   CoreFoundation                      0x000000010ce32f34 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3   CoreFoundation                      0x000000010cd48c15 ___forwarding___ + 1013
4   CoreFoundation                      0x000000010cd48798 _CF_forwarding_prep_0 + 120
5   MapKit                              0x0000000109af40a3 CreatePathForPolygon.38297 + 57
6   MapKit                              0x0000000109af3e4c -[MKPolygonRenderer createPath] + 128
7   MapKit                              0x0000000109aeefda -[MKOverlayPathRenderer drawMapRect:zoomScale:inContext:] + 72
8   MapKit                              0x0000000109aefd89 __47-[MKOverlayRenderer overlay:drawKey:inContext:]_block_invoke + 671
9   MapKit                              0x0000000109aefdda _worldsForBounds.37697 + 58
10  MapKit                              0x0000000109aef99f -[MKOverlayRenderer overlay:drawKey:inContext:] + 224
11  VectorKit                           0x00000001170e0534 __40-[VKRasterOverlayTileSource _queueDraw:]_block_invoke + 484
12  libdispatch.dylib                   0x000000010de3a980 _dispatch_call_block_and_release + 12
13  libdispatch.dylib                   0x000000010de640cd _dispatch_client_callout + 8
14  libdispatch.dylib                   0x000000010de43499 _dispatch_queue_override_invoke + 1733
15  libdispatch.dylib                   0x000000010de453b7 _dispatch_root_queue_drain + 720
16  libdispatch.dylib                   0x000000010de4508b _dispatch_worker_thread3 + 123
17  libsystem_pthread.dylib             0x000000010e20d4de _pthread_wqthread + 1129
18  libsystem_pthread.dylib             0x000000010e20b341 start_wqthread + 13
)
libc++abi.dylib: terminating with uncaught exception of type NSException

通过实验,我发现可以添加圆形叠加而不会使应用程序崩溃,如果它位于未在屏幕上显示的地图的一部分中。一旦用户平移到圆圈所在的地图部分,就会发生此崩溃。

在iOS 10.1和iOS 9.0上使用模拟器时发生错误。

任何关于为什么会发生这种情况的想法将不胜感激。

ios objective-c mkmapview mkcircle
2个回答
0
投票

谢谢大家的意见。最后的问题是因为方法签名不正确:

- (MKOverlayRenderer *)rendererForOverlay:(id<MKOverlay>)overlay

应该是:

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay

这并没有引起更广泛的问题,因为在代码的其他地方有正确签名的方法的另一个实现。该方法试图将MKCircle视为MKPolygon,因此触发了pointCount问题。


0
投票

对于swift 4.2。添加以下委托。我正在向MapView添加Circle和PolyLine。所以需要在委托中处理。

  func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    if overlay is MKCircle {
      let renderer = MKCircleRenderer(overlay: overlay)
      renderer.fillColor = UIColor.black.withAlphaComponent(0.3)
      renderer.strokeColor = UIColor.red
      renderer.lineWidth = 1
      return renderer
    }
    let polyLine = MKPolylineRenderer.init(overlay: overlay)
    polyLine.strokeColor = UIColor.green
    return polyLine
  }
© www.soinside.com 2019 - 2024. All rights reserved.