我正在尝试使用叠加(MKOverlay)跟踪MKMapView上的路线.但是,根据当前的速度,如果颜色正在变化(例如,如果用户从65mph驱动到30mph),我想要在跟踪路线时使用渐变来执行类似Nike应用程序的操作(例如,从绿色变为橙色).
这是我想要的截图:

因此,每隔20米,我使用以下方法添加从旧坐标到新坐标的叠加:
// Create a c array of points.
MKMapPoint *pointsArray = malloc(sizeof(CLLocationCoordinate2D) * 2);
// Create 2 points.
MKMapPoint startPoint = MKMapPointForCoordinate(CLLocationCoordinate2DMake(oldLatitude, oldLongitude));
MKMapPoint endPoint = MKMapPointForCoordinate(CLLocationCoordinate2DMake(newLatitude, newLongitude));
// Fill the array.
pointsArray[0] = startPoint;
pointsArray[1] = endPoint;
// Erase polyline and polyline view if not nil.
if (self.routeLine != nil)
self.routeLine = nil;
if (self.routeLineView != nil)
self.routeLineView = nil;
// Create the polyline based on the array of points.
self.routeLine = [MKPolyline polylineWithPoints:pointsArray count:2];
// Add …Run Code Online (Sandbox Code Playgroud) 在应用程序中,我绘制了一个弯曲的UIBezierPath和一个MKOverlayPathView类来显示航线.这是我正在使用的代码:
Run Code Online (Sandbox Code Playgroud)- (UIBezierPath *)pathForOverlayForMapRect:(MKMapRect)mapRect { ... bla bla bla ... UIBezierPath *path = [UIBezierPath bezierPath]; [path moveToPoint:s]; [path addQuadCurveToPoint:e controlPoint:cp1]; [path addLineToPoint:e2]; [path addQuadCurveToPoint:s2 controlPoint:cp2]; [path closePath]; return path; }
- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context{
self.mapRect = mapRect;
CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0);
CGContextSetLineWidth(context, mapRect.size.height/700);
CGContextSetLineJoin(context, kCGLineJoinRound);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextAddPath(context, [self pathForOverlayForMapRect:mapRect].CGPath);
[self updateTouchablePathForMapRect:mapRect];
CGContextDrawPath(context, kCGPathFillStroke);
}
Run Code Online (Sandbox Code Playgroud)
这工作得很好,但我想在该路径上绘制一个渐变,而不仅仅是填充颜色.这就是它开始变得非常棘手的地方.
我已经尝试过CGContextDrawLinearGradient(),但它还没有让我有用.