如何在 .satelliteFlyover 模式下绘制宽度恒定的折线?

问题描述 投票:0回答:0

我用 mapView.mapType 设置为 .satelliteFlyover 绘制多段线。使用通用的 MKOverlayRenderer 方法,线条的宽度不是恒定的。

ChatGPT 提出了一些实际有效的代码……在某种程度上。我不得不修改几行,但它做了我想做的事。它是 MKPolylineRenderer 的子类:

class CustomPolylineRenderer: MKPolylineRenderer {
    override func draw(_ mapRect: MKMapRect, zoomScale: MKZoomScale, in context: CGContext) {
        super.draw(mapRect, zoomScale: zoomScale, in: context)
        let path = UIBezierPath(cgPath: self.path)
        context.addPath(path.cgPath)
        context.setStrokeColor(UIColor.green.cgColor)
        let lineWidth: CGFloat = 170
        context.setLineWidth(lineWidth)
        context.strokePath()
    }
}

创建多段线后,使用 mapView 委托调用它:

    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
        let polyline = overlay as? MKPolyline

            return CustomPolylineRenderer(overlay: polyline!)
}

这很好用,但问题是我无法弄清楚如何更改 CustomPolylineRenderer 类中线条的宽度或颜色。所以我的问题真的是如何创建一条在使用 .satelliteFlyover 时不会改变宽度的折线?

swift xcode width mapkit
© www.soinside.com 2019 - 2024. All rights reserved.