将MKOverlayPathRenderer作为叠加层添加到MKMapView会发生异常 [英] Adding MKOverlayPathRenderer as overlay to MKMapView gets exception

查看:335
本文介绍了将MKOverlayPathRenderer作为叠加层添加到MKMapView会发生异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有内容都在主题中,所以这里是代码

Everything said in topic, so here is the code

- (void)viewDidLoad
{
     [super viewDidLoad];
     // Do any additional setup after loading the view, typically from a nib.
     _pathRenderer = [[MKOverlayPathRenderer alloc] init];
     _pathRenderer.lineWidth = 8.0f;
     _pathRenderer.strokeColor = [UIColor redColor];
     _pathRenderer.path = CGPathCreateMutable();
     [_mapView addOverlay:_pathRenderer];
}

在最后一行中,它掉落,但有异常:

At the last line it drops with exception:

  Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MKOverlayPathRenderer boundingMapRect]: unrecognized selector 

这意味着我使用了不实现MKOverlay的错误类,我明白了,但是正如MKOverlayPathRenderer的参考中所说-确实如此.因此,我对此问题有些困惑.

It means that i'm using wrong class that don't implements MKOverlay, I got it, but as said in reference of MKOverlayPathRenderer - it does. So I'm a bit stuck with this problem.

推荐答案

MKOverlayPathRenderer未实现MKOverlay协议.

addOverlay需要一个符合MKOverlay协议的对象.

The addOverlay requires an object that conforms to the MKOverlay protocol.

您提供的对象不会那样做,因此您会得到该异常(实现MKOverlay的对象必须具有boundingMapRect属性).

The object you're giving it doesn't do that and so you get that exception (objects that implement MKOverlay must have a boundingMapRect property).

在您的问题中,声明:

正如在MKOverlayPathRenderer的参考中所述-确实如此

as said in reference of MKOverlayPathRenderer - it does

没有道理.

文档没有说MKOverlayPathRenderer实现了MKOverlay. MKOverlayPathRendererMKOverlayRendererNSObject的子类.它仅符合NSObject协议.

The documentation does not say that MKOverlayPathRenderer implements MKOverlay. MKOverlayPathRenderer is a subclass of MKOverlayRenderer and NSObject. It conforms only to the NSObject protocol.


MKOverlayPathRenderer绘制符合MKOverlay的某些 model 覆盖对象的 visual 表示形式.


An MKOverlayPathRenderer draws the visual representation of some model overlay object that conforms to MKOverlay.

因此需要两个单独的对象(类似于注释的工作方式):

So there are two separate objects required (similar to how annotations work):

  1. 覆盖层的模型-实现MKOverlay的对象.
  2. 叠加层的视图-MKOverlayRenderer(或iOS 7之前的MKOverlayView)的某些子类.
  1. The model of the overlay -- something that implements MKOverlay.
  2. The view of the overlay -- some subclass of MKOverlayRenderer (or MKOverlayView before iOS 7).

过程是首先使用addOverlay:addOverlays:方法为MKMapView模型提供 对象.

The procedure is to first give the MKMapView the model object(s) using the addOverlay: or addOverlays: methods.

然后在rendererForOverlay委托方法中(地图视图实际上要显示某些叠加层时将调用该方法),您创建并返回 renderer ( view )用于有问题的叠加层.

Then in the rendererForOverlay delegate method, which the map view will call when it actually wants to display some overlay, you create and return a renderer (view) for the overlay in question.


创建渲染器的代码通常位于rendererForOverlay委托方法中,并且应使用initWithOverlay方法(而不是init),并应传递要为其创建渲染器的overlay模型对象


The code you have that creates a renderer would normally be in the rendererForOverlay delegate method and should use the initWithOverlay method (instead of init) and should pass the overlay model object for which you want to create the renderer.

对于addOverlay,您将创建一些覆盖模型对象-一些标准类,例如MKPolylineMKPolygonMKCircle或自定义类.

For the addOverlay, you would create some overlay model object -- either some standard class like MKPolyline, MKPolygon, MKCircle, or a custom class.


但是您确定需要MKOverlayPathRenderer吗?


But are you sure you need an MKOverlayPathRenderer?

如果只想绘制简单的线,圆或多边形,请使用已经提供的渲染器,这些渲染器会自动为您绘制这些对象.比创建自己的MKOverlayPathRenderer子类要容易得多.

If you just want to draw a simple line, circle, or polygon, use the renderers already provided that automatically draw these objects for you. You will have a much easier time than creating your own subclass of MKOverlayPathRenderer.

MKPolylineRendererMKPolygonRendererMKCircleRendererMKOverlayPathRenderer的内置子类,可以在不编写任何绘图代码的情况下绘制其相关的模型叠加层.

MKPolylineRenderer, MKPolygonRenderer, and MKCircleRenderer are built-in subclasses of MKOverlayPathRenderer that draw their related model overlays without you writing any drawing code.

这篇关于将MKOverlayPathRenderer作为叠加层添加到MKMapView会发生异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆