<分区>
我的应用需要能够获取用户的起始位置,然后计算距该点(所有方向)的特定距离的纬度/经度坐标。有谁知道我如何实现这一点?
<分区>
我的应用需要能够获取用户的起始位置,然后计算距该点(所有方向)的特定距离的纬度/经度坐标。有谁知道我如何实现这一点?
最佳答案
我在这个链接 Moving a CLLocation by x meters 上找到了答案并将其移植到 Swift 中。
func locationWithBearing(bearing:Double, distanceMeters:Double, origin:CLLocationCoordinate2D) -> CLLocationCoordinate2D {
let distRadians = distanceMeters / (6372797.6) // earth radius in meters
let lat1 = origin.latitude * M_PI / 180
let lon1 = origin.longitude * M_PI / 180
let lat2 = asin(sin(lat1) * cos(distRadians) + cos(lat1) * sin(distRadians) * cos(bearing))
let lon2 = lon1 + atan2(sin(bearing) * sin(distRadians) * cos(lat1), cos(distRadians) - sin(lat1) * sin(lat2))
return CLLocationCoordinate2D(latitude: lat2 * 180 / M_PI, longitude: lon2 * 180 / M_PI)
}
Morgan Chen 写道:
All of the math in this method is done in radians. At the start of the method, lon1 and lat1 are converted to radians for this purpose as well. Bearing is in radians too. Keep in mind this method takes into account the curvature of the Earth, which you don't really need to do for small distances.
关于ios - 计算距离另一对纬度/经度坐标特定距离的纬度/经度坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26490313/