我正在尝试继承 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);
}
}