Swift MapKit渲染问题
因此,我有一个应用程序,根据用户的速度在地图上绘制一条多段线,但我有一些问题 首先,有时大量的线条是一种颜色,即使给定的速度变化,它也应该改变 其次,如果用户移动地图或缩放,则整条线变为红色 最后,我得到了一个奇怪的点,看起来它和渲染有关? 编程非常新,所以它可能非常明显 地图视图:Swift MapKit渲染问题,swift,mkmapview,polyline,renderer,Swift,Mkmapview,Polyline,Renderer,因此,我有一个应用程序,根据用户的速度在地图上绘制一条多段线,但我有一些问题 首先,有时大量的线条是一种颜色,即使给定的速度变化,它也应该改变 其次,如果用户移动地图或缩放,则整条线变为红色 最后,我得到了一个奇怪的点,看起来它和渲染有关? 编程非常新,所以它可能非常明显 地图视图: func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer { if overla
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()
}
func地图视图(uMapView:MKMapView,叠加渲染器:MKOverlay)->MKOverlayRenderer{
如果覆盖是多段线{
polylineRenderer=MKPolylineRenderer(覆盖:覆盖)
polylineRenderer.lineWidth=5
如果currentMPH>=0&¤tMPH=10&¤tMPH=30&¤tMPH,则必须创建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委托使用它:
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MyPolyline(overlay: overlay)
renderer.strokeColor = UIColor.black
renderer.lineWidth = 3
return renderer
}
您必须创建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委托使用它:
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MyPolyline(overlay: overlay)
renderer.strokeColor = UIColor.black
renderer.lineWidth = 3
return renderer
}