0

嗨,我编写了这段代码来在某个点之间绘制一条折线:

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 来转换值。

现在的问题是它只在坐标上画点我怎么能连接呢?

4

1 回答 1

1

您正在创建具有单个点的多条多段线,而不是具有多个点的单条多段线。在不知道您的字典结构的情况下很难使代码正确,但这应该更符合您的目标:

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
}
于 2017-06-03T17:52:13.643 回答