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.
MKOverlayPathRendererdoes not implement theMKOverlayprotocol.The
addOverlayrequires an object that conforms to theMKOverlayprotocol.The object you're giving it doesn't do that and so you get that exception (objects that implement
MKOverlaymust have aboundingMapRectproperty).In your question, the statement:
doesn't make sense.
The documentation does not say that
MKOverlayPathRendererimplementsMKOverlay.MKOverlayPathRendereris a subclass ofMKOverlayRendererandNSObject. It conforms only to theNSObjectprotocol.An
MKOverlayPathRendererdraws 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(orMKOverlayViewbefore iOS 7).The procedure is to first give the
MKMapViewthe model object(s) using theaddOverlay:oraddOverlays:methods.Then in the
rendererForOverlaydelegate 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
rendererForOverlaydelegate method and should use theinitWithOverlaymethod (instead ofinit) and should pass theoverlaymodel 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, andMKCircleRendererare built-in subclasses ofMKOverlayPathRendererthat draw their related model overlays without you writing any drawing code.