MKPolylineRenderer produces jagged, unequal paths

后端 未结 6 743
天涯浪人
天涯浪人 2020-12-17 22:10

I am using the iOS 7 MapKit APIs to produce 3D camera movements on a map that displays an MKDirectionsRequest-produced path. The path is rendered by MKOverlayRenderer like s

6条回答
  •  青春惊慌失措
    2020-12-17 22:39

    Subclass MKPolylineRenderer and override applyStrokePropertiesToContext:atZoomScale: so that it ignores the scale, and draws lines at constant width:

    @interface ConstantWidthPolylineRenderer : MKPolylineRenderer
    @end
    
    @implementation ConstantWidthPolylineRenderer
    
    - (void)applyStrokePropertiesToContext:(CGContextRef)context
                               atZoomScale:(MKZoomScale)zoomScale
    {
        [super applyStrokePropertiesToContext:context atZoomScale:zoomScale];
        CGContextSetLineWidth(context, self.lineWidth);
    }
    
    @end
    

    Now use it and admire its smooth rendering:

    - (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id)overlay
    {
        MKPolyline *polyline = (MKPolyline *)overlay;
        ConstantWidthPolylineRenderer *renderer = [[ConstantWidthPolylineRenderer alloc] initWithPolyline:polyline];
        renderer.strokeColor = [UIColor redColor];
        renderer.lineWidth = 40;
        return renderer;
    }
    

提交回复
热议问题