将MKCircleOverlay添加到MKMapView

时间:2016-11-07 01:50:52

标签: ios objective-c mkmapview mkcircle

我正在使用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上使用模拟器时会发生错误。

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

2 个答案:

答案 0 :(得分:0)

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

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

应该是:

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

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

答案 1 :(得分:0)

快速4.2。添加以下代表。我将Circle和PolyLine添加到MapView。因此需要在委托中同时处理。

  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
  }