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
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
does not implement theMKOverlay
protocol.The
addOverlay
requires an object that conforms to theMKOverlay
protocol.The object you're giving it doesn't do that and so you get that exception (objects that implement
MKOverlay
must have aboundingMapRect
property).In your question, the statement:
doesn't make sense.
The documentation does not say that
MKOverlayPathRenderer
implementsMKOverlay
.MKOverlayPathRenderer
is a subclass ofMKOverlayRenderer
andNSObject
. It conforms only to theNSObject
protocol.An
MKOverlayPathRenderer
draws the visual representation of some model overlay object that conforms toMKOverlay
.So there are two separate objects required (similar to how annotations work):
MKOverlay
.MKOverlayRenderer
(orMKOverlayView
before iOS 7).The procedure is to first give the
MKMapView
the model object(s) using theaddOverlay:
oraddOverlays:
methods.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.The code you have that creates a renderer would normally be in the
rendererForOverlay
delegate method and should use theinitWithOverlay
method (instead ofinit
) and should pass theoverlay
model object for which you want to create the renderer.For the
addOverlay
, you would create some overlay model object -- either some standard class likeMKPolyline
,MKPolygon
,MKCircle
, or a custom class.But are you sure you need an
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
, andMKCircleRenderer
are built-in subclasses ofMKOverlayPathRenderer
that draw their related model overlays without you writing any drawing code.