动态更改mkpolyline的lineWidth

Net*_*emo 8 mkmapview ios mkpolyline

我的应用程序中有Map,根据用户位置移动.我已成功绘制了源和目标的折线.使用以下代码

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id < MKOverlay >)overlay{

    MKPolylineView *view1 = [[MKPolylineView alloc] initWithOverlay:overlay];

        view1.lineWidth = 27.0;
        view1.strokeColor = [UIColor colorWithRed:55.0/255.0 green:168.0/255.0 blue:219.0/255.0 alpha:1];

    return view1;
}
Run Code Online (Sandbox Code Playgroud)

但我的问题是当地图移动时,意味着我在用户移动时改变地图的区域,然后有时候折线显示不好,有时它的显示比实际尺寸更厚,如下图所示.

我正在附上下面的图片,请让我知道在地图移动时我可以为平滑折线做些什么.

在此输入图像描述

编辑

正如Matt建议我创建一个MKPolylineRenderer的子类并实现drawMapRect方法,如下所示:

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


    CGMutablePathRef fullPath = CGPathCreateMutable();
    BOOL pathIsEmpty = YES;

    //merging all the points as entire path
    for (int i=0;i< self.polyline.pointCount;i++){
        CGPoint point = [self pointForMapPoint:self.polyline.points[i]];
        if (pathIsEmpty){
            CGPathMoveToPoint(fullPath, nil, point.x, point.y);
            pathIsEmpty = NO;
        } else {
            CGPathAddLineToPoint(fullPath, nil, point.x, point.y);
        }
    }

    //get bounding box out of entire path.
    CGRect pointsRect = CGPathGetBoundingBox(fullPath);
    CGRect mapRectCG = [self rectForMapRect:mapRect];
    //stop any drawing logic, cuz there is no path in current rect.
    if (!CGRectIntersectsRect(pointsRect, mapRectCG))return;

    UIColor *darker = [UIColor blackColor];
    CGFloat baseWidth = 10 / zoomScale;

    // draw the dark colour thicker
    CGContextAddPath(context, self.path);
    CGContextSetStrokeColorWithColor(context, darker.CGColor);
    CGContextSetLineWidth(context, baseWidth * 1.5);
    CGContextSetLineCap(context, self.lineCap);
    CGContextStrokePath(context);

    // now draw the stroke color with the regular width
    CGContextAddPath(context, self.path);
    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
    CGContextSetLineWidth(context, baseWidth);
    CGContextSetLineCap(context, self.lineCap);
    CGContextStrokePath(context);

    [super drawMapRect:mapRect zoomScale:zoomScale inContext:context];
}
Run Code Online (Sandbox Code Playgroud)

但同样的问题 见下图:

在此输入图像描述

好吧,我想我遇到了问题,我的折线加了一次,但是作为用户的速度我改变了MKMapView的缩放级别,缩放级别改变了,但折线宽度没有刷新,

那怎样才能动态改变mkpolyline的lineWidth?

Kam*_*pai 1

你需要使用MKPolylineRenderer而不是MKPolylineView

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id < MKOverlay >)overlay {
    @try {

        MKPolylineRenderer *renderer = [[[MKPolylineRenderer alloc] initWithOverlay:overlay];
        renderer.lineWidth = 27.0;
        renderer.strokeColor = [UIColor colorWithRed:55.0/255.0 green:168.0/255.0 blue:219.0/255.0 alpha:1];

        return renderer;
    }
    @catch (NSException *exception) {
        NSLog(@"exception :%@",exception.debugDescription);
    }
}
Run Code Online (Sandbox Code Playgroud)

读一下这个:

MKPolylineRenderer 类提供 MKPolyline 叠加对象的视觉表示。该渲染器仅描画线条;它没有填满它。您可以通过修改从父类继承的属性来更改多边形的颜色和其他绘制属性。您通常按原样使用此类,而不是对其进行子类化。

来自苹果库

MKPolylineView 类提供 MKPolyline 注释对象的视觉表示。该视图描画注释所表示的路径。(此类不会填充路径所包围的区域。)您可以通过修改从 MKOverlayPathView 类继承的属性来更改路径的颜色和其他绘制属性。该类通常按原样使用,而不是子类化。

来自苹果库