防止MKPolygon有结

时间:2015-03-31 16:01:42

标签: ios objective-c mkpolyline mkpolygon

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

我的问题是如何绘制带结的多边形(见图)(我不知道 knot 是否是正确的词)。我没有找到阻止多边形得到结的简单方法。 对于附加图像的情况,我希望删除小卷曲,甚至要平滑轮廓

你知道一种方法吗?

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 个答案:

答案 0 :(得分:6)

  1. 自iOS 7.0以来,MKOverlayPathView为deprecated。您使用MKOverlayRenderer代替它,还使用相关的地图委托方法。
  2. 尝试使用MKOverlayRenderer的miterLimit属性。
  3. 示例:

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