【发布时间】:2016-02-18 15:41:23
【问题描述】:
我正在尝试获取一个点的坐标,即距起始位置一定距离,但最终结果是错误的。
首先,我计算起始位置和所需目的地之间的角度:
private func calculateAngleBetweenLocations(currentLocation: CLLocationCoordinate2D, targetLocation: CLLocationCoordinate2D) -> Double {
let fLat = self.degreesToRadians(currentLocation.latitude);
let fLng = self.degreesToRadians(currentLocation.longitude);
let tLat = self.degreesToRadians(targetLocation.latitude);
let tLng = self.degreesToRadians(targetLocation.longitude);
let deltaLng = tLng - fLng
let y = sin(deltaLng) * cos(tLat)
let x = cos(fLat) * sin(tLat) - sin(fLat) * cos(tLat) * cos(deltaLng)
let bearing = atan2(y, x)
return self.radiansToDegrees(bearing)
}
然后,我计算新点,给定距离:
private func coordinatesForMovement(endLocation: CLLocationCoordinate2D, distance: Double) -> CLLocationCoordinate2D {
let angle = self.calculateAngleBetweenLocations(self.currentLocation, targetLocation: endLocation)
let x = self.currentLocation.latitude + distance * cos(angle)
let y = self.currentLocation.longitude + distance * sin(angle)
return CLLocationCoordinate2D(latitude: x, longitude: y)
}
this 是结果(脚是起始位置,蓝色标记是目的地,红色标记是新计算点的位置)。我尝试过以米和公里为单位的距离以及所有其他浮点位置,但从未得到正确的结果。有任何想法吗?
【问题讨论】:
-
coordinatesForMovement似乎是混合维度。currentCoordinate.latitude是角度(以度为单位?),distance是长度,cos()是无量纲的。所以程序在长度上加上一个角度,这总是错误的。角度增加角度,长度增加长度。 -
好的,那我该如何解决呢?