问题描述
所有内容都在主题中,所以这里是代码
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
实现了MKOverlay
. MKOverlayPathRenderer
是MKOverlayRenderer
和NSObject
的子类.它仅符合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):
- 覆盖层的模型-实现
MKOverlay
的对象. - 叠加层的视图-
MKOverlayRenderer
(或iOS 7之前的MKOverlayView
)的某些子类.
- The model of the overlay -- something that implements
MKOverlay
. - The view of the overlay -- some subclass of
MKOverlayRenderer
(orMKOverlayView
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
,您将创建一些覆盖模型对象-一些标准类,例如MKPolyline
,MKPolygon
,MKCircle
或自定义类.
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
.
MKPolylineRenderer
,MKPolygonRenderer
和MKCircleRenderer
是MKOverlayPathRenderer
的内置子类,可以在不编写任何绘图代码的情况下绘制其相关的模型叠加层.
MKPolylineRenderer
, MKPolygonRenderer
, and MKCircleRenderer
are built-in subclasses of MKOverlayPathRenderer
that draw their related model overlays without you writing any drawing code.
这篇关于将MKOverlayPathRenderer作为叠加层添加到MKMapView会发生异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!