ios - MKPolygonRenderer 不填充多边形(大部分时间)

标签 ios swift mapkit

我正在将几个多边形叠加层绘制到 map View 上。奇怪的是,虽然总是绘制多边形线,但大多数时候多边形并没有被填充。尽管有时他们是,正如我所期望的那样......

我正在使用 Swift 3,针对 iOS 9.3 和 10。代码如下所示:

func addPolygons() {
    for field in self.fields! {
        if field.polygon.count > 0 {
            let polygon = LisFieldPolygon(field: field)
            self.mapView.add(polygon)
        }
    }
    // Add circle for location accuracy
    if (self.location != nil) {
        let circle = MKCircle(center: self.location!.coordinate, radius: self.location!.horizontalAccuracy)
        self.mapView.add(circle)
    }
}

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    switch overlay {
    case is MKPolygon:
        let polyView = MKPolygonRenderer(overlay: overlay)
        let color = UIColor.init(red: 0xf6/255, green: 0x81/255, blue: 0x3c/255, alpha: 1.0)
        polyView.strokeColor = color
        polyView.fillColor = color.withAlphaComponent(0.2)
        polyView.lineWidth = 1.0
        return polyView
    case is LisFieldPolygon:
        let lisOverlay = overlay as! LisFieldPolygon
        let polyView = MKPolygonRenderer(overlay: lisOverlay.polygon)
        let color: UIColor
        if lisOverlay.field.make.is_farmland {
            color = UIColor.init(red: 0x0e/255, green: 0xd5/255, blue: 0x1d/255, alpha: 1.0)
        } else {
            color = UIColor.init(red: 0xf6/255, green: 0x81/255, blue: 0x3c/255, alpha: 1.0)
        }
        polyView.fillColor = color // .withAlphaComponent(0.5)
        polyView.strokeColor = color
        polyView.lineWidth = 1.0
        return polyView
    case is MKCircle:
        let circleView = MKCircleRenderer(overlay: overlay)
        let color = UIColor(red: 0.1, green: 0.3, blue: 0.6, alpha: 1.0)
        circleView.strokeColor = color.withAlphaComponent(0.8)
        circleView.fillColor = color.withAlphaComponent(0.2)
        circleView.lineWidth = 1.0
        return circleView
    case is MKTileOverlay:
        let tileRenderer = MKTileOverlayRenderer(tileOverlay: self.tileOverlay!)
        return tileRenderer
    default:
        printError("LisMapViewController.mapView rendererForOverlay: Unexpected overlay")
        let lineView = MKPolylineRenderer(overlay: overlay)
        lineView.strokeColor = UIColor.red
        return lineView
    }
}

如果我使用 MKPolygon 而不是我自己的 LisFieldPolygon 类绘制多边形,我会得到相同的结果:轮廓,但(大部分时间)没有填充。

这让我感到困惑...我看不出我做错了什么,尤其是当代码看起来像我发现的类似示例时。我需要刷新一些东西吗?我是否错过了注册某些东西,按特定顺序放置某些东西? (顺便说一句,问题与 Swift 2.3 相同)

无论使用内部 map 、卫星 View 还是 OSM map (使用 MKTileOverlay),结果都是一样的。

PS:MKCircle 已经填满了。始终如一。

最佳答案

好的,经过大量的调试和尝试不同的事情,我终于找到了解决方案。 上面的代码工作正常。问题出在数据上。多边形数据通过网络从数据库同步。我犯了一个错误,没有删除坐标数组,而是一直附加坐标。这导致每个多边形的多组坐标(基本上描述了原始多边形周围的多个圆)。这似乎无法正确填充多边形...

如果我只有一组坐标,填充工作正常。因此,如果其他人遇到类似问题,请仔细检查您的坐标数据;-)

关于ios - MKPolygonRenderer 不填充多边形(大部分时间),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39588655/

相关文章:

ios - 非消耗品应用内购买收据验证是否可选?

swift - 是否可以在初始化时直接改变属性/变量?

swift - 如何让函数中的这个按钮用 Swift 打开另一个 View Controller ?

ios - 尝试以 swift 2 重载错误的形式读取 JSON 文件

ios - 在自定义注释之上显示用户个人资料图像

ios - 如何在不点击 map 的情况下在 map 上显示图钉的标注?

ios - Swift 3 协议(protocol)的可选绑定(bind)

ios - 如何显示youtube视频文件的下载进度

ios - 已批准 future 付款的用户的 PayPal 电子邮件 ID

ios - 用于打开其他应用程序的 canOpenURL - 不适用于模拟器