问题描述
我正在使用iOS 7 MapKit API在地图上生成3D相机移动,该地图显示MKDirectionsRequest生成的路径。该路径由MKOverlayRenderer呈现,如下所示:
I am using the iOS 7 MapKit APIs to produce 3D camera movements on a map that displays an MKDirectionsRequest-produced path. The path is rendered by MKOverlayRenderer like so:
-(void)showRoute:(MKDirectionsResponse *)response
{
for (MKRoute *route in response.routes)
{
[self.map
addOverlay:route.polyline level:MKOverlayLevelAboveRoads];
}
}
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id < MKOverlay >)overlay
{
MKPolylineRenderer *renderer =
[[MKPolylineRenderer alloc] initWithOverlay:overlay];
UIColor *mapOverlayColor = [UIColor colorWithRed:((float)22 / 255.0f) green:((float)126 / 255.0f) blue:((float)251 / 255.0f) alpha:0.8];
renderer.strokeColor = mapOverlayColor;
renderer.lineWidth = 13.0;
return renderer;
}
除一个问题外,它运作良好。当我使用MKMapCameras缩放或平移路径时(如果没有它们,如果我只是以用户身份执行),则路径呈锯齿状,如此屏幕截图所示:
It's working well except for one issue. When I zoom or pan around the path with MKMapCameras (and without them, if I simply do so as the user), the path is jagged as shown in this screenshot:
我测试过是否切换到MKOverlayLevelAboveLabels有所不同,但遗憾的是结果是相同的。
I tested to see if switching to MKOverlayLevelAboveLabels makes a difference but sadly the outcome was the same.
有没有人有关于如何改进渲染的建议?切换到测地路径是否会产生影响?如果是这样,我将如何在此实现?
Does anyone have suggestions as to how to improve the rendering? Does switching to a geodesic path make a difference and if so, how would I implement this here?
推荐答案
子类MKPolylineRenderer并覆盖applyStrokePropertiesToContext :atZoomScale:这样它会忽略比例,并以恒定宽度绘制线条:
Subclass MKPolylineRenderer and override applyStrokePropertiesToContext:atZoomScale: so that it ignores the scale, and draws lines at constant width:
@interface ConstantWidthPolylineRenderer : MKPolylineRenderer
@end
@implementation ConstantWidthPolylineRenderer
- (void)applyStrokePropertiesToContext:(CGContextRef)context
atZoomScale:(MKZoomScale)zoomScale
{
[super applyStrokePropertiesToContext:context atZoomScale:zoomScale];
CGContextSetLineWidth(context, self.lineWidth);
}
@end
现在使用它并欣赏它平滑渲染:
Now use it and admire its smooth rendering:
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
MKPolyline *polyline = (MKPolyline *)overlay;
ConstantWidthPolylineRenderer *renderer = [[ConstantWidthPolylineRenderer alloc] initWithPolyline:polyline];
renderer.strokeColor = [UIColor redColor];
renderer.lineWidth = 40;
return renderer;
}
这篇关于MKPolylineRenderer产生锯齿状,不相等的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!