0

我正在尝试继承 MKPolylineRenderer 以便在 mapkit 中制作自定义路线图。我尝试遵循自定义地图路径线

但无论我做什么,我的路径都不会出现在我的地图上。我是 CG 新手,所以我可能会遗漏一些简单的东西。我可以从折线转到 cg,还是需要将折线转换为 CGPoints?

在我的地图视图中,我调用我的子类

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
if ([overlay isKindOfClass:[MKPolyline class]]) {
    MKPolyline *route = overlay;
    //MKPolylineRenderer *routeRenderer = [[MKPolylineRenderer alloc] initWithPolyline:route];
    CustomPathOverlayRenderer *routeRenderer = [[CustomPathOverlayRenderer alloc] initWithPolyline:route];
    //routeRenderer.strokeColor = [UIColor blueColor];
    //routeRenderer.lineWidth = 4;

    return routeRenderer;
}
    return nil;
}

然后我有我的 CustomPathOverlayRenderer.m

- (id)initWithPolyline:(MKPolyline *)polyline
{
    if (self = [super initWithPolyline:polyline])
{
    //
}
    return self;
}


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

CGMutablePathRef path = CGPathCreateMutable();
if (path != nil)
{

    CGPathRef  path2 = CGPathCreateCopy(path);

    //[line.color getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha];

    //UIColor *c2 =[UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:0.4];
    CGContextAddPath(context, path);

    CGContextSetStrokeColorWithColor(context, [UIColor purpleColor].CGColor);
    CGContextSetLineJoin(context, kCGLineJoinRound);
    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineWidth(context, MKRoadWidthAtZoomScale(zoomScale));
    CGContextStrokePath(context);
    CGPathRelease(path);

    CGContextSetBlendMode(context, kCGBlendModeSourceAtop);
    CGContextAddPath(context, path2);
    CGContextSetRGBStrokeColor(context, 1.0f, 0, 0, 0.3f);
    CGContextSetLineJoin(context, kCGLineJoinRound);
    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineWidth(context, MKRoadWidthAtZoomScale(zoomScale)/2.0f);
    CGContextStrokePath(context);
    CGPathRelease(path2);

    }

}
4

1 回答 1

0

想通了,我确实需要将折线添加到 CGPath

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;
于 2014-05-13T18:00:16.920 回答