我正在 MKMapView 上测试一些基本的绘图功能,并且发生了一些奇怪的事情。您可以引用下面的保管箱链接。这是在实际设备上运行和测试的 - 而不是模拟器。
您会注意到一些 MKPolyLines 在连接时呈锯齿状,而另一些则没有。造成这种情况的原因是什么?可以纠正吗?我没有看到任何设置抗锯齿的选项。我认为最右边的多边形是你最能看到的。但总的来说,似乎有些线条比其他线条更平滑。
此屏幕截图未显示的另一件事是,每次我点击 View 添加新的叠加层时, map 上的所有叠加层都会“闪烁”,就好像它们正在重新绘制或可能没有快速重新绘制一样足够的。想知道这是什么原因,或者也许我应该做其他事情来防止这种情况发生,我不知道。
还有一个关于委托(delegate)的问题。如果您的 map 上有 100 个不同的叠加层怎么办?您是否应该对委托(delegate)中的每一个进行检查,以告诉它要执行哪种绘图?就我而言,只有 3 个案例,但我可以想象这个方法可能会因为我在这里设置的内容而变得非常大。我知道这是一个旁白问题,但我想我应该把它插在这里。
https://www.dropbox.com/s/dbordqnml33urmu/Screenshot%202013.03.28%2011.26.36.png
相关代码如下:
-(void)addCoordinateToList:(UITapGestureRecognizer*)recognizer{
CGPoint tappedPoint = [recognizer locationInView:map];
CLLocationCoordinate2D coord = [map convertPoint:tappedPoint toCoordinateFromView:map];
NSLog(@"Coordinate Info: Lat %f, Lon %f", coord.latitude, coord.longitude);
CLLocation *newLocation = [[CLLocation alloc] initWithLatitude:coord.latitude longitude:coord.longitude];
//Extension NSValue new for iOS6
//[path addObject:[NSValue valueWithMKCoordinate:coord]];
[path addObject:newLocation];
[self drawPolygon];
clearPolygonFromMap.enabled = YES;
clearPolygonFromMap.alpha = 1.0;
//add annotations
[self setPolygonAnnotationWithCoordinate:coord andWithTitle:@"default title"];
}
//绘制折线。设置多边形,但在完成形状绘制之前不要添加为叠加层。不包含此代码,因为这里有很多不相关的按钮状态。
-(void)drawPolygon{
NSInteger numberOfCoordinates = path.count;
if(coords != NULL)
free(coords);
coords = malloc(sizeof(CLLocationCoordinate2D) * numberOfCoordinates);
CLLocationCoordinate2D coordinates[numberOfCoordinates];
for(int pathIndex = 0; pathIndex < numberOfCoordinates; pathIndex++){
CLLocation *location = [path objectAtIndex:pathIndex];
coords[pathIndex] = location.coordinate;
coordinates[pathIndex] = coords[pathIndex];
}
polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfCoordinates];
polygon = [MKPolygon polygonWithCoordinates:coords count:path.count];
[map addOverlay:polyLine];
}
//设置圆圈叠加
-(void)setPolygonAnnotationWithCoordinate:(CLLocationCoordinate2D)coord andWithTitle:(NSString *)polygonDescription{
circle = [MKCircle circleWithCenterCoordinate:coord radius:15];
circle.title = polygonDescription;
[map addOverlay:circle];
//[map addAnnotation:circle];
}
//MapView覆盖委托(delegate)方法。有没有更好的方法来检查覆盖?如果我有 100 个叠加层怎么办
-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay{
if(overlay == circle){
pointCircleView = [[MKCircleView alloc]initWithOverlay:overlay];
pointCircleView.strokeColor = [UIColor greenColor];
pointCircleView.fillColor = [UIColor redColor];
//circleView.alpha = 0.5;
pointCircleView.lineWidth = 4.0;
return pointCircleView;
}
else if(overlay == polyLine){
borderView = [[MKPolylineView alloc] initWithPolyline:overlay];
borderView.strokeColor = [UIColor blackColor];
borderView.lineWidth = 1.0;
return borderView;
}
else if(overlay == polygon){
polygonView = [[MKPolygonView alloc] initWithPolygon:polygon];
polygonView.strokeColor = [UIColor blackColor];
polygonView.lineWidth = 1.0;
polygonView.fillColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.0 alpha:0.4];
return polygonView;
}
}
最佳答案
对于闪烁,请参阅此 post - 这可能是覆盖系统中的一个错误。
对于锯齿线,它们在屏幕截图中看起来确实是抗锯齿的,但可能已按比例放大。根据实验,我相信叠加层会渲染为离散缩放级别的单独位图,然后在放大或缩小的 map View 上渲染以适应当前的缩放级别。因此,您的叠加层在该级别上看起来可能有点粗糙,但放大或缩小会有所改善。
关于委托(delegate),您可以检查传入的覆盖层的类,以降低 mapView:viewForOverlay
函数的复杂性,而不是检查特定的覆盖层。所以:
if ([overlay isKindOfClass:[MKPolyline class]])
{
borderView = [[MKPolylineView alloc] initWithPolyline:overlay];
borderView.strokeColor = [UIColor blackColor];
borderView.lineWidth = 1.0;
return borderView;
}
关于ios - 添加新叠加层时,MKMapView 叠加层重绘(闪烁)。 MKPolyLines 呈锯齿状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15688055/