自定义叠加层(渲染器)被地图图块切割(在某些情况下)

custom overlay(renderer) is getting cut of by map tiles (in some cases)

我写了一个自定义渲染器来表示最小和最大半径。在某些情况下,渲染器未按预期工作。叠加层似乎被地图图块切断了。


See the full video

这是我的做法。我错过了什么吗?

class RadiusOverlayRenderer: MKOverlayRenderer {

    override func draw(_ mapRect: MKMapRect, zoomScale: MKZoomScale, in context: CGContext) {

        guard let overlay = self.overlay as? RadiusOverlay else {
            return
        }

        let maxRadiusRect = self.rect(for: overlay.boundingMapRect)
            .offsetBy(
                dx: CGFloat(-overlay.boundingMapRect.height)/2,
                dy: CGFloat(-overlay.boundingMapRect.width)/2
            )

        let minRadiusRect = CGRect(
            x: Double(maxRadiusRect.midX)-overlay.minRadRect.width/2,
            y: Double(maxRadiusRect.midY)-overlay.minRadRect.height/2,
            width: overlay.minRadRect.width,
            height: overlay.minRadRect.height)

        let aPath = CGMutablePath()
        aPath.addEllipse(in: maxRadiusRect)
        aPath.addEllipse(in: minRadiusRect)
        aPath.closeSubpath()

        context.setFillColor(overlay.color.cgColor)
        context.setAlpha(overlay.alpha)
        context.addPath(aPath)
        context.drawPath(using: .eoFillStroke)

    }

}

注意到只有左上部分被剪掉了吗?

.offsetBy 你在 .boundingMapRect 之外绘图。

删除 .offsetBy...

如果您想在不同的地方画圆圈,请调整 MKOverlaycoordinate 和/或 boundingMapRect