简体   繁体   English

动态更改mkpolyline的lineWidth

[英]dynamically change lineWidth of mkpolyline

I have Map in my app in which is moving as per user location.I have successfully drawn a polyline for source and destination. 我的应用程序中有地图,该地图正在根据用户位置移动。我已经成功绘制了源和目标的折线。 Using following code 使用以下代码

- (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;
}

But my problem is when map is moving , mean i am changing region of map as user moves , then some times polyline is not shown good some time its show thicker than actual size as you can see in below image. 但是我的问题是地图移动时,意味着我随着用户移动而改变了地图的区域,然后有时折线显示得不好,有时它显示的要比实际大小粗,如下图所示。

i am attaching the image below, please let me know what can i do for smooth polyline when map is moving. 我附上下面的图像,请让我知道地图移动时如何处理平滑的折线。

在此处输入图片说明

EDIT 编辑

As Matt suggested i create a subclass of MKPolylineRenderer and implement drawMapRect method as below: 正如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];
}

But Same problem See below image: 但是同样的问题,见下图:

在此处输入图片说明

ok, i think i got problem , my polyline is added once, but as user's speed i changing zoom level of MKMapView, zoom level is changed , but polyline width is not refresh, 好的,我认为我遇到了问题,我的折线只添加了一次,但是随着用户更改MKMapView缩放级别的速度,缩放级别发生了变化,但是折线宽度没有刷新,

SO how can i dynamically change lineWidth of mkpolyline ?? 所以我怎么能动态改变mkpolyline的lineWidth?

The problem is that you are setting your lineWidth at a fixed width (a really big fixed width). 问题是您将lineWidth设置为固定宽度(很大的固定宽度)。 This gets larger or smaller as the map is zoomed larger or smaller. 随着地图放大或缩小,该比例会变大或变小。

Instead, implement your own MKOverlayRenderer subclass, and override drawMapRect:zoomScale:inContext: . 而是实现自己的MKOverlayRenderer子类,并重写drawMapRect:zoomScale:inContext: :。 It receives a zoomScale parameter, and so you can adjust the width of your line to look good at what the current scale may be. 它收到一个zoomScale参数,因此您可以调整线条的宽度,使其看起来好于当前的缩放比例。

Subclass MKPolylineRenderer and override applyStrokePropertiesToContext:atZoomScale: so that it ignores the scale, and draws lines at constant width: 子类MKPolylineRenderer并重写applyStrokePropertiesToContext:atZoomScale:以便它忽略比例,并以恒定宽度绘制线:

@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<MKOverlay>)overlay
{
    MKPolyline *polyline = (MKPolyline *)overlay;
    ConstantWidthPolylineRenderer *renderer = [[ConstantWidthPolylineRenderer alloc] initWithPolyline:polyline];
    renderer.strokeColor = [UIColor redColor];
    renderer.lineWidth = 40;
    return renderer;
}

You need to use MKPolylineRenderer instead of MKPolylineView 您需要使用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);
    }
}

Read this one: 阅读此内容:

The MKPolylineRenderer class provides the visual representation for an MKPolyline overlay object. MKPolylineRenderer类提供MKPolyline覆盖对象的视觉表示。 This renderer strokes the line only; 此渲染器仅描边线条; it does not fill it. 它不能填充它。 You can change the color and other drawing attributes of the polygon by modifying the properties inherited from the parent class. 您可以通过修改从父类继承的属性来更改多边形的颜色和其他图形属性。 You typically use this class as is and do not subclass it. 您通常按原样使用此类,并且不要对其进行子类化。

From Apple library 从苹果图书馆

The MKPolylineView class provides the visual representation for an MKPolyline annotation object. MKPolylineView类为MKPolyline注释对象提供视觉表示。 This view strokes the path represented by the annotation. 该视图描画了由注释表示的路径。 (This class does not fill the area enclosed by the path.) You can change the color and other drawing attributes of the path by modifying the properties inherited from the MKOverlayPathView class. (此类不会填充路径所包围的区域。)您可以通过修改从MKOverlayPathView类继承的属性来更改路径的颜色和其他图形属性。 This class is typically used as is and not subclassed. 此类通常按原样使用,而不是子类。

From Apple library 从苹果图书馆

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM