How can can I get equidistant location on a MKPolyline?

大城市里の小女人 提交于 2019-12-11 18:07:17

问题


I need to find equidistant locations on a MKPolyline to add annotations as shown below.

My function to get locations on MKPolyline is given below, I have the values start and end coordinates of Polyline. But the locations are slightly ,moving out of polyline as shown in the image below

My function to find location is

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
    }

In viewdidload

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

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

How can I solve this error in calculating locations?


回答1:


The problem is that the earth is round. So your "line" is not a line; it is a curve traced out on the surface of a nominal sphere. You cannot find "equidistant" points along the line using your simple-minded method. You need to use some much more serious math than you are using. This will prove to be far from trivial.



来源:https://stackoverflow.com/questions/49944054/how-can-can-i-get-equidistant-location-on-a-mkpolyline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!