MapKit渲染问题 [英] MapKit Rendering Issues
问题描述
所以我有一个应用程序,可以根据用户的速度在地图上绘制一条折线,但是我有一些问题.
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.
最后,我得到一个看起来像与渲染有关的怪异点? http://imgur.com/a/o5AKf
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()
}
推荐答案
您必须创建MKPolylineRenderer的子类
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)
}
}
}
然后在渲染器中使用它用于MapKit委托:
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
}
这篇关于MapKit渲染问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!