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 overlay to map.
[self.mapView addOverlay:self.routeLine];
// clear the memory allocated earlier for the points.
free(pointsArray);
// Save old coordinates.
oldLatitude = newLatitude;
oldLongitude = newLongitude;
基本上,我会添加很多小的叠加层。然后,我想在此小线条图上创建渐变,因此我尝试在叠加委托中这样做:
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay {
MKOverlayView* overlayView = nil;
if(overlay == self.routeLine) {
// If we have not yet created an overlay view for this overlay, create it now.
if(self.routeLineView == nil) {
self.routeLineView = [[[MKPolylineView alloc] initWithPolyline:self.routeLine] autorelease];
if (speedMPH < 25.0) {
self.routeLineView.fillColor = [UIColor redColor];
self.routeLineView.strokeColor = [UIColor redColor];
}
else if (speedMPH >= 25.0 && speedMPH < 50.0) {
self.routeLineView.fillColor = [UIColor orangeColor];
self.routeLineView.strokeColor = [UIColor orangeColor];
}
else {
self.routeLineView.fillColor = [UIColor greenColor];
self.routeLineView.strokeColor = [UIColor greenColor];
}
// Size of the trace.
self.routeLineView.lineWidth = routeLineWidth;
// Add gradient if color changed.
if (oldColor != self.routeLineView.fillColor) {
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.routeLineView.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[oldColor CGColor], (id)[self.routeLineView.fillColor CGColor], nil];
[self.routeLineView.layer insertSublayer:gradient atIndex:0];
}
// Record old color for gradient.
if (speedMPH < 25.0)
oldColor = [UIColor redColor];
else if (speedMPH >= 25.0 && speedMPH < 50.0)
oldColor = [UIColor orangeColor];
else
oldColor = [UIColor greenColor];
}
overlayView = self.routeLineView;
}
return overlayView;
}
我正在尝试以这种方式添加渐变,但是我猜这不是做到这一点的方法,因为我无法使其正常工作。
每当用户位置(在位置对象的委托中)或每20米以上有一次更新时,我还可以跟踪路线。
您能帮我解决这个问题,给我提示!
谢谢!
没有找到相关结果
已邀请:
3 个回复
薄响
方法时使用渐变对其进行描边,因为在ios7中将
替换为
。 我试图通过将ѭ5子类化来实现这一点,但是我没有挑选出单独的CGPath,然后发现了一个名为
的神秘方法,听起来像我需要的,但是如果您在不调用super方法时不会调用它覆盖您的
。 那就是我目前正在解决的问题。 我会继续努力,因此,如果我解决了一些问题,我会回来并更新答案。 =========更新======================================== ======== 所以这就是我最近要解决的问题,我几乎实现了上面提到的基本思想,但是是的,我仍然无法根据特定的mapRect挑选单个PATH,因此,当所有路径的boundingBox与当前mapRect相交。技巧很差,但现在就可以工作。 在render类的
方法中,我这样做:
然后,我逐点拆分整个路径以分别绘制其渐变。 请注意,包含色调值的“ 10”数组映射每个位置的速度。
这就是所有核心绘制方法,当然,您的叠加层类中需要
和
并将它们输入CLLocationManager。 最后一点是如何从速度中获得
值,好吧,我发现如果色调范围从0.03到0.3准确地表示从红色到绿色,那么我对色调和速度做了一些比例映射。 最后一个,这里是这个演示的完整资源:https://github.com/wdanxna/GradientPolyline 如果看不到您画的线,不要惊慌,我只是将地图区域放在我的位置:)
蕉衫
GradidentPolylineRenderer
如何使用 从CLLocations数组创建一行
在委托中传递GradientPolylineRenderer
结果
膏焦凑