gpt4 book ai didi

Swift,MKPolyline 如何在坐标点之间创建折线

转载 作者:行者123 更新时间:2023-11-28 15:35:28 28 4
gpt4 key购买 nike

你好,我写了这段代码来在一些点之间画一条折线:

var arrayToDraw: Array<Any> = []
var forpolyline: Array<CLLocationDegrees> = []
var forpolyline2: CLLocationCoordinate2D = CLLocationCoordinate2D.init()


func showRoute() {
for h in 0...(orderFinalDictionary.count - 1){
arrayToDraw = orderFinalDictionary[h].value
print(arrayToDraw)
var arrayToDrawCount = arrayToDraw.count
for n in 0...(arrayToDrawCount - 1){
forpolyline = (arrayToDraw[n] as! Array<CLLocationDegrees>)
forpolyline2.latitude = (forpolyline[0])
forpolyline2.longitude = (forpolyline[1])
print(forpolyline2)
var geodesic = MKPolyline(coordinates: &forpolyline2, count: 1)
self.mapView.add(geodesic)
}
}
}

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolylineRenderer(polyline: overlay as! MKPolyline)
renderer.strokeColor = UIColor.red
renderer.lineWidth = 3

return renderer
}

它从字典中获取坐标,将其放入数组 (arraToDraw) 中,然后我使用 forpolyline 和 forpolyline2 来转换值。

现在的问题是它只在坐标上画点,我该如何连接它?

最佳答案

您正在创建具有单个点的多条折线,而不是具有多个点的单个折线。在不知道你的字典结构的情况下很难得到正确的代码,但这应该更符合你正在尝试做的事情:

var arrayToDraw: Array<Any> = []
var forpolyline: Array<CLLocationDegrees> = []
var forpolyline2: CLLocationCoordinate2D = [CLLocationCoordinate2D]()

func showRoute() {
for h in 0...(orderFinalDictionary.count - 1){
arrayToDraw = orderFinalDictionary[h].value
print(arrayToDraw)
var arrayToDrawCount = arrayToDraw.count
for n in 0...(arrayToDrawCount - 1){
forpolyline = (arrayToDraw[n] as! Array<CLLocationDegrees>)
forpolyline2.append(CLLocationCoordinate2D(latitude: forpolyline[0], longitude: forpolyline[1]))
}
print(forpolyline2)
let geodesic = MKPolyline(coordinates: &forpolyline2, count: forpolyline2.count)
self.mapView.add(geodesic)
}
}

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolylineRenderer(polyline: overlay as! MKPolyline)
renderer.strokeColor = UIColor.red
renderer.lineWidth = 3

return renderer
}

关于Swift,MKPolyline 如何在坐标点之间创建折线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44346688/

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