【发布时间】:2015-01-06 21:06:51
【问题描述】:
我的应用程序中有地图,它根据用户位置移动。我已经成功地为源和目标绘制了一条折线。 使用以下代码
- (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;
}
但我的问题是当地图移动时,意味着我在用户移动时更改地图区域,然后有时折线显示不佳,有时显示比实际尺寸厚,如下图所示。
我附上下面的图片,请告诉我在地图移动时我可以为平滑折线做些什么。
编辑
正如马特建议的那样,我创建了一个 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];
}
但同样的问题 见下图:
好的,我想我遇到了问题,我的折线添加了一次,但是随着用户的速度我改变了 MKMapView 的缩放级别,缩放级别也改变了,但是折线宽度没有刷新,
那么我如何动态改变 mkpolyline 的 lineWidth ??
【问题讨论】:
-
在 iOS 10 中使用 MKPolylineRenderer 时出现同样的问题。您找到解决方案了吗?
-
我遇到了同样的问题,还没有找到任何解决方案。
-
我也面临同样的问题,任何解决方案都可以根据放大或缩小来管理折线宽度@Kumar
-
在撰写本文时,iOS 11.3.1 没有找到有效的解决方案。我尝试了 StackOverflow 上建议的几种解决方案,但都没有奏效。有消息吗?
标签: ios mkmapview mkpolyline