怎样才能在MKPolyline上获得等距位置?

时间:2018-04-20 14:31:44

标签: ios swift google-maps google-maps-api-3 mkmapview

我需要在MKPolyline上找到等距位置来添加注释,如下所示。

enter image description here

我在MKPolyline上获取位置的功能如下所示,我有Polyline的起始和终点坐标值。但是位置略微偏离折线,如下图所示

enter image description here

我找到位置的功能是

func getEquidistantPoints(from startPoint: CLLocationCoordinate2D, to endPoint: CLLocationCoordinate2D, numberOfPoints: Int) -> [CLLocationCoordinate2D] {

        var midPoints: [CLLocationCoordinate2D] = []
        var newPoint: CLLocationCoordinate2D = CLLocationCoordinate2DMake(0, 0)

        let count = numberOfPoints + 1
        let latitudeModifier = (endPoint.latitude - startPoint.latitude) / Double(count)
        let longitudeModifier = (endPoint.longitude - startPoint.longitude) / Double(count)

        for i in 0..<count {
            newPoint.latitude = CLLocationDegrees(startPoint.latitude + (latitudeModifier * Double(i)))
            newPoint.longitude = CLLocationDegrees(startPoint.longitude + (longitudeModifier * Double(i)))
            midPoints.append(newPoint)
        }
        return midPoints
    }

在viewdidload中

let coordinatesArray = getEquidistantPoints(from: sourceCoordinate, to: destinationCoordinate, numberOfPoints: 5)

        for coordinate in coordinatesArray {
            let annotation = MKPointAnnotation()
            annotation.coordinate = coordinate
            self.mapView.addAnnotation(annotation)
        }

如何在计算位置时解决此错误?

1 个答案:

答案 0 :(得分:0)

问题是地球是圆的。所以你的“线”不是一条线;它是在标称球体表面上描绘出的曲线。使用简单的方法,您无法在线上找到“等距”点。您需要使用比您使用的更严肃的数学。这将证明远非微不足道。