1

I try to subclass from MKOverlayPathRenderer and implement -createPath

- (void)createPath
{
    MKPolyline *line = (id)self.overlay;

    MKMapPoint *points = line.points;
    NSUInteger pointCount = line.pointCount;

    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, points[0].x, points[0].y);

    for (int i = 1; i < pointCount; i++) {
        CGPathAddLineToPoint(path, NULL, points[i].x, points[i].y);
    }
    [self setPath:path];
}

I create overlay here:

CLLocationCoordinate2D coordinates[events.count];
for (int i; i < events.count; i++) {
    coordinates[i] = [events[i] coordinate];
}

MKPolyline *line = [MKPolyline polylineWithCoordinates:coordinates count:events.count];
[mapView addOverlay:line];

And then create renderer here:

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(MKPolyline *)overlay
{
    MKBezierPathRenderer *r = [[MKBezierPathRenderer alloc] initWithOverlay:overlay];
    r.lineWidth = 8.f;
    r.strokeColor = [UIColor redColor];
    r.fillColor = [UIColor redColor];

    return r;
}

But I can't see any lines on map. What should I do? Thanx.

P.S. CGPathAddLineToPoint is for tests now, in production I need curves.

AlKozin
  • 904
  • 8
  • 25
  • The point values of the path are not supposed to be MKMapPoints but in a different set of units. Use the pointForMapPoint method to convert. See http://stackoverflow.com/questions/19941200/looking-for-an-mkoverlaypathrenderer-example for an example. –  Feb 08 '15 at 17:11
  • @Anna thank you! I miss it. Can you post your comment as answer please? – AlKozin Feb 10 '15 at 14:28
  • Thanks but go ahead and post an answer with your updated code. You can then accept it after some time. –  Feb 10 '15 at 14:50

1 Answers1

2

According to Anna's answer you should use [self pointForMapPoint:points[i]] instead of points[i]

- (void)createPath
{
    MKPolyline *line = (id)self.overlay;

    MKMapPoint *points = line.points;
    NSUInteger pointCount = line.pointCount;

    CGMutablePathRef path = CGPathCreateMutable();
    CGPoint point = [self pointForMapPoint:points[0]];
    CGPathMoveToPoint(path, NULL, point.x, point.y);

    for (int i = 1; i < pointCount; i++) {
        point = [self pointForMapPoint:points[i]];
        CGPathAddLineToPoint(path, NULL, point.x, point.y);
    }
    [self setPath:path];
}
AlKozin
  • 904
  • 8
  • 25
  • If you are interested, there's a curved line example: https://stackoverflow.com/a/61573384/4260691 – OhadM May 03 '20 at 11:09