向MKPolyline

时间:2016-10-14 17:22:27

标签: mkmapview core-graphics mkpolyline

我试图将方向箭头添加到MKPolyline,我几乎得到了它,但由于某种原因,一些箭头被切断(见下面的屏幕截图)。我在使用Core Graphics进行绘图方面表现不佳,所以我的猜测就是它的内容。任何人都有关于如何处理剪切箭头的任何指示? MKPolyline arrows

以下代码使用子类化MKPolylineRenderer的drawMapRect:zoomScale:inContext:方法绘制图形:

  MKMapPoint prevMapPoint = mapPoints[0];
  MKMapPoint mapPoint = mapPoints[1];
  for (NSUInteger i = 1; i < pointCount; i++) {
    mapPoint = _mapPoints[i];
    CGPoint prevCGPt = [self pointForMapPoint:prevMapPoint];
    CGPoint cgPoint = [self pointForMapPoint:mapPoint];
    CGFloat bearing = atan2(cgPoint.y - prevCGPt.y, cgPoint.x - prevCGPt.x) - M_PI;

    //Get other two corners of triangle
    CGFloat arrowAngle = degreesToRadians(40.0);
    CGPoint pt2 = PointAtBearingFromPoint(cgPoint, bearing+arrowAngle/2, arrowLength);
    CGPoint pt3 = PointAtBearingFromPoint(cgPoint, bearing-arrowAngle/2, arrowLength);

    //Draw triangle and fill
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, cgPoint.x, cgPoint.y); //go to tip of triangle
    CGContextAddLineToPoint(context, pt2.x, pt2.y);
    CGContextAddLineToPoint(context, pt3.x, pt3.y);
    CGContextClosePath(context);
    CGContextFillPath(context);
  }

  prevMapPoint = mapPoint;

(同样在drawMapRect:zoomScale:inContext:方法中是一个For循环来决定放置每个箭头的位置并填充mapPoints数组,但我假设这不是问题)。

0 个答案:

没有答案