MKPolyline strange rendering related with zooming in MapKit

后端 未结 1 1988
既然无缘
既然无缘 2021-02-05 21:34

I have very simple View Controller to demonstrate this strange rendering behavior of MKPolyline. Nothing special just normal api calls.

import UIKit
import MapKi         


        
1条回答
  •  执笔经年
    2021-02-05 21:58

    Swift 3 solution :

    Create a subclass of MKPolylineRenderer

    class CustomPolyline: MKPolylineRenderer {
    
        override func applyStrokeProperties(to context: CGContext, atZoomScale zoomScale: MKZoomScale) {
            super.applyStrokeProperties(to: context, atZoomScale: zoomScale)
            UIGraphicsPushContext(context)
            if let ctx = UIGraphicsGetCurrentContext() {
                ctx.setLineWidth(self.lineWidth)
            }
        }
    }
    

    Then use it in your rendererFor MapKit delegate :

    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
            let renderer = CustomPolyline(overlay: overlay)
            renderer.strokeColor = UIColor.red
            renderer.lineWidth = 100
            return renderer
    }
    

    Your polylines won't re-render after zooming thus avoiding the artifacts

    0 讨论(0)
提交回复
热议问题