如何获得MKPolyline上的等距位置? [英] How can can I get equidistant location on a MKPolyline?
问题描述
我需要在MKPolyline上找到等距的位置以添加注释,如下所示.
I need to find equidistant locations on a MKPolyline to add annotations as shown below.
下面给出了我在MKPolyline上获取位置的函数,我具有Polyline的开始和结束坐标值.但是位置略有变化,如下图所示
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
我找到位置的功能是
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
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?
推荐答案
问题是地球是圆形的.因此,您的行"不是行;它是在标称球体表面上绘制的曲线.您不能使用简单的方法在直线上找到等距"点.您需要使用比您使用的更为严格的数学.这将证明是微不足道的.
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.
这篇关于如何获得MKPolyline上的等距位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!