我正在尝试子类化MKPolylineRenderer,以便在mapkit中制作自定义路线图。 我尝试了Custom map path line
但无论我做什么,我的路径都没有出现在我的地图上。我是CG的新手,所以我可能会遗漏一些简单的东西。 我可以从折线转到cg,还是需要将折线转换为CGPoints?
在我的mapview中,我调用了我的子类
- (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);
}
}
答案 0 :(得分: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;