gpt4 book ai didi

ios - 绘制 MKPolyline 填充颜色

转载 作者:可可西里 更新时间:2023-11-01 03:36:34 26 4
gpt4 key购买 nike

我正在尝试在 MKPolylineView 上绘制漂亮的 MKPolyline。到目前为止一切都很顺利 - 多段线按照我想要的方式绘制,并在我希望它使用以下代码时绘制:

[[self map] addOverlay:routeLine];

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

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

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

进一步调查后,我在 Xcode 的快速帮助部分找到了以下信息:

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(那到底是什么?)但这似乎是解决方案 - 有人知道如何使用它吗?

最佳答案

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

// 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。如果您在 map 中添加了多个叠加层,这会更灵活一些。


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

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

这样我就可以将这三种类型的叠加层添加到我的 map 中,并且它可以正确渲染所有这些。

关于ios - 绘制 MKPolyline 填充颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14915226/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com