绘制MKPolyline填充颜色 [英] Draw MKPolyline Fill Color

查看:144
本文介绍了绘制MKPolyline填充颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在MKPolylineView上绘制漂亮的MKPolyline.到目前为止,一切进展顺利-折线的绘制就如我所愿,并且在需要时使用以下代码进行绘制:

I am attempting to draw a nice MKPolyline on an MKPolylineView. Everything is going great so far - the polyline draws just as I want it to and when I want it to using the following code:

[[self map] addOverlay:routeLine];

这个方法告诉应用程序如何绘制它:

And this method to tell the app how to draw it:

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay {
    MKOverlayView* overlayView = nil;
    self.routeLineView = [[MKPolylineView alloc] initWithPolyline:[self routeLine]];
    [[self routeLineView] setFillColor:[UIColor colorWithRed:167/255.0f green:210/255.0f blue:244/255.0f alpha:1.0]];
    [[self routeLineView] setStrokeColor:[UIColor colorWithRed:0/255.0f green:136/255.0f blue:255/255.0f alpha:1.0]];
    [[self routeLineView] setLineWidth:15.0];
    [[self routeLineView] setLineCap:kCGLineCapRound];
    overlayView = [self routeLineView];
    return overlayView;
}

结果,我得到了一条带有纯蓝色的线.但是,我看到的蓝色不是我期望的笔触的填充颜色-它是笔触颜色的蓝色.当我使用这种方法时,没有填充色. 为什么不在折线上绘制填充颜色?

As a result I get a line with a solid blue. The blue I see is not however the fill color with the stroke I am expecting - it is the blue of the stroke color. There is no fill color when I use this method. Why doesn't it draw the fill color on the polyline?

经过进一步调查,我在Xcode的快速帮助部分中找到了这些信息:

After investigating further, I found this tidbit of information in the Quick Help section of Xcode:

MKPolylineView类为MKPolyline注释对象提供视觉表示.该视图描画了由注释表示的路径. (此类未填充路径包围的区域.)您可以通过修改从MKOverlayPathView类继承的属性来更改路径的颜色和其他图形属性.

The MKPolylineView class provides the visual representation for an MKPolyline annotation object. 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(仍然是什么?),但是这似乎是解决方案-有人知道如何使用它吗?

That just sounds ridiculous. I have to use this class to set the fill color, but I can't use this class to draw the fill color? That just seems very odd considering it already draws the stroke. The last line in this explanation from the documentation is a little unclear but seems to provide an answer - I'm just having a hard time coding / finding the answer. I don't have an MKOverlayPathView in my project (what is that anyway?) but it seems to be the solution - does anyone know how to use it?

推荐答案

如果您想要一种颜色的线,另一种颜色的线,请使用MKPolygon而不是MKPolyline.因此,请相应地修改注释的原始创建.然后修改viewForOverlay(或者,对于iOS 7,为rendererForOverlay)以识别MKPolygon并执行以下操作:

If you want a line of one color, and a fill of another, use a MKPolygon instead of a MKPolyline. So modify your original creation of the annotation accordingly. And then you modify your viewForOverlay (or, for iOS 7, rendererForOverlay) to recognize the MKPolygon and do something like the following:

// for iOS7+; see `viewForOverlay` for earlier versions

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
    if ([overlay isKindOfClass:[MKPolygon class]])
    {
        MKPolygonRenderer *renderer = [[MKPolygonRenderer alloc] initWithPolygon:overlay];

        renderer.fillColor   = [[UIColor cyanColor] colorWithAlphaComponent:0.2];
        renderer.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];
        renderer.lineWidth   = 3;

        return renderer;
    }

    return nil;
}

// for iOS versions prior to 7; see `rendererForOverlay` for iOS7 and later

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    if ([overlay isKindOfClass:[MKPolygon class]])
    {
        MKPolygonView *overlayView = [[MKPolygonView alloc] initWithPolygon:overlay];

        overlayView.fillColor      = [[UIColor cyanColor] colorWithAlphaComponent:0.2];
        overlayView.strokeColor    = [[UIColor blueColor] colorWithAlphaComponent:0.7];
        overlayView.lineWidth      = 3;

        return overlayView;
    }

    return nil;
}

注意,您无需在此处引用任何类属性,因为叠加层会传递到您的viewForOverlay.万一您在地图上添加了多个叠加层,此方法会更加灵活.

Note, you don't need to reference any of your class properties here, as the overlay is passed to your viewForOverlay. This is a little more flexible in case you ever have multiple overlays added to your map.

顺便说一下,这些是我的标准viewForOverlay(7.0之前的iOS版本)和rendererForOverlay(iOS 7+),它们可以处理MKPolygonMKPolylineMKCircle叠加层:

Incidentally, these are my standard viewForOverlay (iOS versions prior to 7.0) and rendererForOverlay (iOS 7+), which will handle MKPolygon, MKPolyline, and MKCircle overlays:

// for iOS7+; see `viewForOverlay` for earlier versions

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
    if ([overlay isKindOfClass:[MKPolygon class]])
    {
        MKPolygonRenderer *renderer = [[MKPolygonRenderer alloc] initWithPolygon:overlay];

        renderer.fillColor   = [[UIColor cyanColor] colorWithAlphaComponent:0.2];
        renderer.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];
        renderer.lineWidth   = 3;

        return renderer;
    }

    if ([overlay isKindOfClass:[MKCircle class]])
    {
        MKCircleRenderer *renderer = [[MKCircleRenderer alloc] initWithCircle:overlay];

        renderer.fillColor   = [[UIColor cyanColor] colorWithAlphaComponent:0.2];
        renderer.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];
        renderer.lineWidth   = 3;

        return renderer;
    }

    if ([overlay isKindOfClass:[MKPolyline class]])
    {
        MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithPolyline:overlay];

        renderer.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];
        renderer.lineWidth   = 3;

        return renderer;
    }

    return nil;
}

// for iOS versions prior to 7; see `rendererForOverlay` for iOS7 and later

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    if ([overlay isKindOfClass:[MKPolygon class]])
    {
        MKPolygonView *overlayView = [[MKPolygonView alloc] initWithPolygon:overlay];

        overlayView.fillColor      = [[UIColor cyanColor] colorWithAlphaComponent:0.2];
        overlayView.strokeColor    = [[UIColor blueColor] colorWithAlphaComponent:0.7];
        overlayView.lineWidth      = 3;

        return overlayView;
    }

    if ([overlay isKindOfClass:[MKCircle class]])
    {
        MKCircleView *overlayView = [[MKCircleView alloc] initWithCircle:overlay];

        overlayView.fillColor     = [[UIColor cyanColor] colorWithAlphaComponent:0.2];
        overlayView.strokeColor   = [[UIColor blueColor] colorWithAlphaComponent:0.7];
        overlayView.lineWidth     = 3;

        return overlayView;
    }

    if ([overlay isKindOfClass:[MKPolyline class]])
    {
        MKPolylineView *overlayView = [[MKPolylineView alloc] initWithPolyline:overlay];

        overlayView.strokeColor     = [[UIColor blueColor] colorWithAlphaComponent:0.7];
        overlayView.lineWidth       = 3;

        return overlayView;
    }

    return nil;
}

通过这种方式,我可以在地图中添加这三种类型的叠加层中的许多,并且可以正确地渲染所有叠加层.

This way I can add as many of these three types of overlays to my map, and it can render all of them properly.

这篇关于绘制MKPolyline填充颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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