ios – 防止MKPolygon有结

我正在开发一个地图的应用程序,用户可以在其中绘制多边形区域.

我的问题是可以用结打印多边形(见图像)(我不知道结是否是正确的词).我没有找到一个简单的方法来防止多边形得到结.
对于所附图像的情况,我想要小的卷曲被去除,甚至轮廓平滑

你知道一个办法吗?

用户触摸屏幕时绘制多边形的过程使用MKpolyline,MKpolygon和MKOverlay,如下所示:

- (void)touchesBegan:(UITouch*)touch
{
    CGPoint location = [touch locationInView:self.mapView];
    CLLocationCoordinate2D coordinate = [self.mapView convertPoint:location toCoordinateFromView:self.mapView];
    [self.coordinates addobject:[NSValue valueWithMKCoordinate:coordinate]];
}

- (void)touchesMoved:(UITouch*)touch
{
    CGPoint location = [touch locationInView:self.mapView];
    CLLocationCoordinate2D coordinate = [self.mapView convertPoint:location toCoordinateFromView:self.mapView];
    [self.coordinates addobject:[NSValue valueWithMKCoordinate:coordinate]];
}

- (void)touchesEnded:(UITouch*)touch
{
    CGPoint location = [touch locationInView:self.mapView];
    CLLocationCoordinate2D coordinate = [self.mapView convertPoint:location toCoordinateFromView:self.mapView];
    [self.coordinates addobject:[NSValue valueWithMKCoordinate:coordinate]];
    [self didTouchUpInsideDrawButton:nil];
}

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    MKOverlayPathView *overlayPathView;

    if ([overlay isKindOfClass:[MKpolygon class]])
    {
        // create a polygonView using polygon_overlay object
        overlayPathView = [[MKpolygonView alloc] initWithpolygon:overlay];
        overlayPathView.fillColor   = [UIColor redColor];
        overlayPathView.linewidth = 1.5;
        return overlayPathView;
    }
    else if ([overlay isKindOfClass:[MKpolyline class]])
    {
        overlayPathView = [[MKpolylineView alloc] initWithpolyline:(MKpolyline *)overlay];
        overlayPathView.fillColor   = [UIColor redColor];
        overlayPathView.linewidth = 3;
        return overlayPathView;
    }
    return nil;
}

解决方法

>从iOS 7.0起,MKOverlayPathView为 deprecated.您可以使用 MKOverlayRenderer而不是相关的地图委托方法.
>尝试玩与MKOverlayRenderer的 miterLimit属性.

例:

-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {
    if ([overlay isKindOfClass:[MKpolygon class]]) {
        mkpolygonrenderer *polygonRenederer = [[mkpolygonrenderer alloc] initWithpolygon:overlay];
        polygonRenederer.fillColor = [UIColor redColor];
        polygonRenederer.linewidth = 1.5;
        polygonRenederer.miterLimit = 10;
        return polygonRenederer;
    } else if ([overlay isKindOfClass:[MKpolyline class]]) {
        MKpolylineRenderer *lineRenderer = [[MKpolylineRenderer alloc] initWithpolyline:overlay];
        lineRenderer.strokeColor = [UIColor redColor];
        lineRenderer.linewidth = 3;
        return lineRenderer;
    }
    return nil;
}

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...