7

I am trying to figure out how to use a new MKOverlayPathRenderer class.

In my app I previously used MKOverlayPathView when building with iOS 6 SDK, but it does not seem to work with iOS 7 SDK unfortunately.

So I am trying to move my app from MKOverlayPathView to MKOverlayPathRenderer, but have no success so far.

MKPolylineRenderer works OK, but MKOverlayPathRenderer does not. The code gets called, but no overlay is drawn on a map.

Does anybody have a working example for MKOverlayPathRenderer?

rudolph9
  • 8,021
  • 9
  • 50
  • 80
slavb
  • 71
  • 1
  • 3
  • There's a good example here including a curved line instead of solid line: https://stackoverflow.com/a/61573384/4260691 – OhadM May 03 '20 at 11:08

1 Answers1

8

First you have to make sure that you set lineWidth and strokeColor

polylineRenderer.lineWidth = 8.0f;
polylineRenderer.strokeColor = [UIColor redColor];

Then In your renderer class, you have to override -(void) createPath method

-(void) createPath{
    CGMutablePathRef path = CGPathCreateMutable();
    BOOL pathIsEmpty = YES;
    for (int i=0;i< polyline.pointCount;i++){
        CGPoint point = [self pointForMapPoint:polyline.points[i]];
        if (pathIsEmpty){
            CGPathMoveToPoint(path, nil, point.x, point.y);
            pathIsEmpty = NO;
        } else {
            CGPathAddLineToPoint(path, nil, point.x, point.y);
        }
    }

    self.path = path; //<—— don't forget this line.
}

if you want custom drawing, you would like to override this

-(void) drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context method.

wdanxna
  • 10,699
  • 2
  • 23
  • 24
  • I am also trying to use MKOverlayRenderer, could you offer some advice on my question http://stackoverflow.com/questions/20558591/animated-gif-not-working-in-mkmapview-overlay-using-mkoverlayrenderer?noredirect=1#comment30789828_20558591 – wigging Dec 15 '13 at 01:06
  • Even after self.path = path; self.path is still NULL – malhal Aug 22 '14 at 15:34