ios - 防止 MKPolygon 打结

标签 ios objective-c mkpolyline mkpolygon

我正在开发一个带有 map 的应用程序,用户可以在其中绘制多边形区域。

我的问题是可以用节点绘制多边形(见图片)(我不知道节点是否是正确的词)。我没有找到防止多边形打结的简单方法。 对于附图的情况,我希望去除小 curl ,甚至平滑轮廓

你知道怎么做吗?

Polygon with curl

当用户触摸屏幕时绘制多边形的过程确实使用了MKPolylineMKPolygonMKOverlay,如下所示:

- (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;
}

最佳答案

  1. MKOverlayPathView 是 deprecated从 iOS 7.0 开始。你会使用 MKOverlayRenderer而不是它以及相关的 map 委托(delegate)方法。
  2. 尝试玩miterLimit MKOverlayRenderer 的属性。

例子:

-(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;
}

关于ios - 防止 MKPolygon 打结,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29373144/

相关文章:

objective-c - 使用OpenCV和iOS创建封闭区域时将任何图像转换为可着色

ios - 如何在 iOS 中仅选择特定的 c 文件以使用 xCode 进行编译

Swift,MKPolyline 如何在坐标点之间创建折线

ios - 未绘制 Mkpolyline

ios - MKPolyLineRenderer 改变颜色而不移除覆盖

ios - Objective-C block 。上下文捕获相同的变量/复制相同的 NSStackBlock

ios - 子域后的通用链接打开 URL

ios - 如何创建仅 alpha 位图上下文

ios - 如何通过cocoapods从github向项目添加特定文件

ios - 什么是 kCFErrorDomainCFNetwork Code=303