相关疑难解决方法(0)

使用MapKit ios的渐变折线

我正在尝试使用叠加(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)

overlay gradient mapkit mkmapview ios

20
推荐指数
3
解决办法
1万
查看次数

在MKPolyLineView中绘制CAGradient

我的MKPolyLineView只有一个问题.我只是尝试为Polyline创建一个颜色渐变,但是使用CAGradient它会起作用.我将MKPolylineView子类化并重新绘制

- (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];
}
Run Code Online (Sandbox Code Playgroud)

但即使这样也行不通(StrokeColor =红色).有任何想法如何获得折线的渐变?(高色,中心色,低色)

感谢大家.

objective-c calayer ios cagradientlayer

4
推荐指数
1
解决办法
1101
查看次数