ios - 计算距离另一对纬度/经度坐标特定距离的纬度/经度坐标

标签 ios objective-c xcode swift latitude-longitude

<分区>

我的应用需要能够获取用户的起始位置,然后计算距该点(所有方向)的特定距离的纬度/经度坐标。有谁知道我如何实现这一点?

最佳答案

我在这个链接 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/

相关文章:

ios - 子类化 swift 通用可解码类型

objective-c - 如何从 URL 下载图像并将其保存到我的计算机?

ios - OCR Tesseract 只设置数字

ios - paymentQueue RestoreCompletedTransactions 完成 : return queue with empty transactions array

ios - 导航栏中的 pageControl 未居中

iphone - 通过 Iphone 的视频流

iphone - 如何创建 pinterest 样式隐藏/取消隐藏导航/标签栏?

swift - 选择日期选择器单元格时禁用 tableView

objective-c - iOS获取磁盘上的文件大小

ios - 使用 spritekit 和 swift 在游戏中实现短声音