MapKit Rendering Issues

社会主义新天地 提交于 2019-12-11 08:02:23

问题


So I have an application that draws a polyline on the map depending on the speed of the user, but I have some issues with it.

Firstly, sometimes a large amount of the line is one colour even though given speed variations it should be changing.

Secondly, if the user moves the map around or zooms, then the whole line goes red.

And finally, I get this weird dot that looks like it is something to do with rendering? http://imgur.com/a/o5AKf

Pretty new to programming so it might be really obvious!

mapView:

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    if overlay is MKPolyline {
        polylineRenderer = MKPolylineRenderer(overlay: overlay)
        polylineRenderer.lineWidth = 5

        if currentMPH >= 0 && currentMPH <= 9 {
            polylineRenderer.strokeColor = UIColor(red: 1.00, green: 0.12, blue: 0.00, alpha: 1.0)
        }
        if currentMPH >= 10 && currentMPH <= 29 {
            polylineRenderer.strokeColor = UIColor(red: 1.00, green: 0.67, blue: 0.00, alpha: 1.0)
        }
        if currentMPH >= 30 && currentMPH <= 49 {
            polylineRenderer.strokeColor = UIColor(red: 0.03, green: 1.00, blue: 0.01, alpha: 1.0)
        }
        return polylineRenderer
    }
    return MKPolylineRenderer()
}

回答1:


You have to create a subclass of MKPolylineRenderer

class MyPolyline: MKPolylineRenderer {
    override func applyStrokeProperties(to context: CGContext, 
atZoomScale zoomScale: MKZoomScale) {
        super.applyStrokeProperties(to: context, atZoomScale: 
zoomScale)
        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 = MyPolyline(overlay: overlay)
        renderer.strokeColor = UIColor.black
        renderer.lineWidth = 3
        return renderer
}


来源:https://stackoverflow.com/questions/44365262/mapkit-rendering-issues

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!