如何自定义MKPolyLineView来绘制不同的样式线 [英] how to customize MKPolyLineView to draw different style lines

查看:958
本文介绍了如何自定义MKPolyLineView来绘制不同的样式线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想自定义在MKMapView上绘制的线以显示路线,以使线条具有边框颜色和填充颜色。类似这里它有一个黑色边框,并用另一种颜色填充:

I want to customize the lines drawn on MKMapView to show a route so that the lines have a border color and a fill color. Similar to this where it has a black border and is filled with another color:

我目前只是从 mapView中返回MKPolyLineView对象:viewForOverlay:它适用于纯线。文档说MKPolyLineView不是子类,所以我应该继承MKOverlayView和实现我自己的drawMapRect?或者我应该继承MKOverlayPathView?或者创建MKPolylineView的替代品?

I'm currently just returning MKPolyLineView objects from mapView:viewForOverlay: which works fine for plain lines. The docs says the MKPolyLineView is not to be subclassed, so should I subclass MKOverlayView and implement my own drawMapRect? Or should I subclass MKOverlayPathView? Or create a replacement for MKPolylineView?

EDIT - 我要问的是:在哪里放置自己的Quartz绘图代码,以便绘制您自己的注释/覆盖?目前我已经创建了一个MKOverlayView的子类,并实现我自己的drawMapRect:zoomScale:inContext:这很容易绘制覆盖,但是是最好的解决方案?

EDIT - what I'm asking is: where is the place to put your own Quartz drawing code in order to draw your own annotations/overlays? Currently I've created a subclass of MKOverlayView and implement my own drawMapRect:zoomScale:inContext: It's pretty easy to draw the overlay that way but is that the best solution?

推荐答案

您可以通过实现您自己的MKOverlayPathView子类来实现这一点,它在地图rect中绘制路径两次。

You can do this by implementing your own MKOverlayPathView subclass, which draws the path twice in the map rect. Once thicker with black and once thinner on top with another colour.

我创建了一个简单的替换MKPolylineView,让你这样做:ASPolylineView

I have created a simple drop-in replacement of MKPolylineView which lets you do that: ASPolylineView.

如果你想自己做,你需要的两个主要方法实现可能如下所示:

If you want to do it yourself, the two main methods that you need to implement could look like this:

- (void)drawMapRect:(MKMapRect)mapRect
          zoomScale:(MKZoomScale)zoomScale
          inContext:(CGContextRef)context
{
    UIColor *darker = [UIColor blackColor];
    CGFloat baseWidth = self.lineWidth / 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, self.strokeColor.CGColor);
    CGContextSetLineWidth(context, baseWidth);
    CGContextSetLineCap(context, self.lineCap);
    CGContextStrokePath(context);

    [super drawMapRect:mapRect zoomScale:zoomScale inContext:context];
}

- (void)createPath
{
    // turn the polyline into a path

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

    for (int i = 0; i < self.polyline.pointCount; i++) {
        CGPoint point = [self pointForMapPoint:self.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;
}

这篇关于如何自定义MKPolyLineView来绘制不同的样式线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆