【问题标题】:How can can I get equidistant location on a MKPolyline?如何在 MKPolyline 上获得等距位置?
【发布时间】:2018-10-01 07:31:23
【问题描述】:

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

我在 MKPolyline 上获取位置的函数如下所示,我有折线的开始和结束坐标值。但是位置稍微有些偏离折线,如下图所示

我查找位置的功能是

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)
        }

如何解决这个位置计算错误?

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-19
      • 2012-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-18
      相关资源
      最近更新 更多