Ios 绘制多段线填充颜色
我正试图在Ios 绘制多段线填充颜色,ios,objective-c,mkmapview,mapkit,polyline,Ios,Objective C,Mkmapview,Mapkit,Polyline,我正试图在MKPolylineView上绘制一条漂亮的MKPolylineView。到目前为止,一切都进展顺利-多段线的绘制方式与我所希望的一样,并且在我希望的时候使用以下代码: [[self map] addOverlay:routeLine]; 此方法告诉应用程序如何绘制它: - (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay { MKOverlay
MKPolylineView
上绘制一条漂亮的MKPolylineView
。到目前为止,一切都进展顺利-多段线的绘制方式与我所希望的一样,并且在我希望的时候使用以下代码:
[[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;
}
-(MKOverlayView*)地图视图:(MKMapView*)地图视图覆盖:(id)覆盖{
MKOverlayView*overlayView=nil;
self.routeLineView=[[MKPolylineView alloc]initWithPolyline:[self-routeLine]];
[[self-routeLineView]setFillColor:[UIColor-WithRed:167/255.0f-green:210/255.0f-blue:244/255.0f-alpha:1.0];
[[self-routeLineView]设置行程颜色:[UIColor color with red:0/255.0f green:136/255.0f blue:255/255.0f alpha:1.0];
[[self routeLineView]设置线宽:15.0];
[[self routeLineView]setLineCap:kCGLineCapRound];
overlayView=[自路由视图];
返回覆盖视图;
}
结果我得到了一条蓝色实线。然而,我看到的蓝色并不是我期望的笔划填充颜色——它是笔划颜色中的蓝色。使用此方法时没有填充颜色为什么不在多段线上绘制填充颜色?
在进一步调查之后,我在Xcode的“快速帮助”部分发现了以下信息:
MKPolylineView类为MKPolyline注释对象提供可视化表示。此视图绘制注释表示的路径。(此类不会填充路径所包围的区域)您可以通过修改从MKOverlayPathView类继承的特性来更改路径的颜色和其他图形属性
听起来很可笑。我必须使用这个类来设置填充颜色,但是我不能使用这个类来绘制填充颜色?这看起来很奇怪,因为它已经吸引了很多人。文档中的最后一行解释有点不清楚,但似乎提供了一个答案——我只是很难编码/找到答案。我的项目中没有
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;
}
通过这种方式,我可以将这三种类型的叠加添加到我的地图中,并且它可以正确地渲染它们。我找到了另一种方法
如果没有坐标数组,可以绘制两条多段线
绘制特定线宽的第一行,然后绘制另一行,线宽为line2\u=line1\u-width-1.0
这将绘制两条线,第二条线的边距将成为第一条线的笔划。是的,如Rob所述,使用MKPolygon和MKPolygon渲染器,而不是MKPolyline和mkPolyLinerEnder,但还要确保点阵列按顺时针顺序排列。如果它是逆时针顺序,您只需要通过调用数组上的reverse()来反转它
pointsToUse.reverse()
let overlay = MKPolygon(coordinates: &pointsToUse, count: pointsToUse.count)
您可以通过实现自己的MKOverlayPathView子类来实现这一点,该子类在map rect中绘制两次路径。一次用黑色加厚,一次用另一种颜色加薄 我创建了一个简单的MKPolylineView插入式替换,您可以使用它:ASPolylineView
- (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;
}
-(void)drawMapRect:(MKMapRect)mapRect
zoomScale:(MKZoomScale)zoomScale
inContext:(CGContextRef)上下文
{
UIColor*深色=[UIColor blackColor];
CGFloat baseWidth=self.lineWidth/zoomScale;
//把深色画得更浓些
CGContextAddPath(上下文,self.path);
CGContextSetStrokeColorWithColor(上下文,深色.CGColor);
CGContextSetLineWidth(上下文,baseWidth*1.5);
CGContextSetLineCap(上下文,self.lineCap);
CGContextStrokePath(上下文);
//现在绘制具有规则宽度的笔划颜色
CGContextAddPath(上下文,self.path);
CGContextSetStrokeColorWithColor(上下文,self.strokeColor.CGColor);
CGContextSetLineWidth(上下文,baseWidth);
CGContextSetLineCap(上下文,self.lineCap);
CGContextStrokePath(上下文);
[super-drawMapRect:mapRect-zoomScale:zoomScale-inContext:context];
}
-(void)创建路径
{
//将多段线转换为路径
CGMutablePathRef path=CGPathCreateMutable();
BOOL pathIsEmpty=是;
对于(int i=0;i
或者你可以从下面的链接下载代码
我用过它,并在这个屏幕截图上看了看
MKPolylineView
是MKOverlayPathView
的子类。无论如何,您只需要使用MKPolygon
(并创建关联的MKPolygonView
)而不是MKPolygon
MKPolygon
绘制笔划和填充。多边形创建闭合形状,而多段线创建开放形状。是否可以在不关闭形状的情况下执行此操作?我想要绘制一条带有填充和笔划的线,但该线以多边形闭合,并且没有使用多段线填充。添加两个覆盖,一个用于填充(一个没有可见笔划/边框的多边形,例如,一个清晰的笔划颜色
),另一个用于开放笔划(多段线)。
- (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;
}