gpt4 book ai didi

ios - 如何使用不同的笔触/颜色在 MKMapView 上绘制 2 条 MKPolyline?

转载 作者:行者123 更新时间:2023-12-04 07:43:15 26 4
gpt4 key购买 nike

我有一个 MKMapView,我在其中跟踪用户的路径(它是一个正在运行的应用程序),但要求是创建一条具有两种颜色的线。一个用于笔划的中心,另一个用于笔划的边界。为此,我正在实现 UIViewController 类的方法 func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer,以返回渲染器。

我正在使用 Swift 4

有任何想法吗?

提前致谢。

最佳答案

好的,我在写问题的时候找到了解决方案,所以我会告诉你解决方案。

首先,您必须创建两个类,它们扩展了 MKPolyline 类

fileprivate class ForegroundOverlay: MKPolyline{

}
fileprivate class BackgroundOverlay: MKPolyline{

}

其次,您必须修改位置更新时触发的事件
    var positions = [CLLocationCoordinate2D]()
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation:CLLocation = locations[0] as CLLocation

positions.append(userLocation.coordinate)

print("Nuber of locations \(positions.count)")
print("user latitude = \(userLocation.coordinate.latitude)")
print("user longitude = \(userLocation.coordinate.longitude)")

speedIndicator.text = "Speed: \(userLocation.speed * 3.6). Altitude: \(userLocation.altitude)"


let fPolyLine = BackgroundOverlay(coordinates: positions, count: positions.count)

mapView.addOverlays([fPolyLine], level: MKOverlayLevel.aboveRoads)

let bPolyLine = ForegroundOverlay(coordinates: positions, count: positions.count)

mapView.addOverlays([bPolyLine], level: MKOverlayLevel.aboveRoads)

}

第三,您必须询问折线是一类还是另一类。
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolylineRenderer(polyline: overlay as! MKPolyline)
if overlay is ForegroundOverlay {
renderer.strokeColor = UIColor(red: 230/255, green: 230/255, blue: 1, alpha: 0.5)
renderer.lineWidth = 10
} else {
renderer.strokeColor = UIColor(red: 0, green: 0, blue: 1, alpha: 0.5)
renderer.lineWidth = 30
}

return renderer
}

结果看起来像这样

enter image description here

关于ios - 如何使用不同的笔触/颜色在 MKMapView 上绘制 2 条 MKPolyline?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47878313/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com