【问题标题】:Prevent MKPolygon to have knots防止 MKPolygon 打结
【发布时间】:2015-03-31 16:01:42
【问题描述】:

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

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

你知道怎么做吗?

用户在触摸屏幕时绘制多边形的过程,确实使用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;
}

【问题讨论】:

  • 真的,没有人能告诉我我可以遵循哪条轨迹来平滑多边形的边缘?
  • 请提供您创建 MKPolygon 并将其添加到 mapView 的代码

标签: ios objective-c mkpolyline mkpolygon


【解决方案1】:
  1. MKOverlayPathView 自 iOS 7.0 起为 deprecated。您将使用 MKOverlayRenderer 代替它以及相关的地图委托方法。
  2. 尝试使用 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;
}

【讨论】:

  • 感谢您的回复,这似乎是一个不错的选择。我会告诉你我是否能够解决我的问题。
猜你喜欢
  • 2015-12-29
  • 1970-01-01
  • 2019-10-27
  • 1970-01-01
  • 1970-01-01
  • 2014-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多