swift - 如何从CoreLocation中的两个坐标获得对称坐标
解决方案
用法:
class Annotation: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
init(coordinate: CLLocationCoordinate2D) {
self.coordinate = coordinate
}
}
class ViewController: UIViewController {
@IBOutlet weak var map: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
let coord1 = CLLocationCoordinate2D(latitude: 34.1490875, longitude: 74.0789389)
let coord2 = CLLocationCoordinate2D(latitude: 28.619570, longitude: 77.088104)
map.addAnnotation(Annotation(coordinate: coord1))
map.addAnnotation(Annotation(coordinate: coord2))
let loc1 = CLLocation(latitude: coord1.latitude, longitude: coord1.longitude)
let loc2 = CLLocation(latitude: coord2.latitude, longitude: coord2.longitude)
// Distance
let distance = loc1.distance(from: loc2)
print(distance)
// Bearing
let bearing = getBearingBetweenTwoPoints1(point1: loc2, point2: loc1)
print(bearing)
let coord3 = getNewTargetCoordinate(position: coord1, userBearing: Float(bearing), distance: Float(distance))
map.addAnnotation(Annotation(coordinate: coord3))
print(coord3)
}
}
参考: https ://stackoverflow.com/a/52831007/6576315 https://stackoverflow.com/a/27004436/6576315
extension ViewController {
func degreesToRadians(degrees: Double) -> Double { return degrees * .pi / 180.0 }
func radiansToDegrees(radians: Double) -> Double { return radians * 180.0 / .pi }
func getBearingBetweenTwoPoints1(point1 : CLLocation, point2 : CLLocation) -> Double {
let lat1 = degreesToRadians(degrees: point1.coordinate.latitude)
let lon1 = degreesToRadians(degrees: point1.coordinate.longitude)
let lat2 = degreesToRadians(degrees: point2.coordinate.latitude)
let lon2 = degreesToRadians(degrees: point2.coordinate.longitude)
let dLon = lon2 - lon1
let y = sin(dLon) * cos(lat2)
let x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon)
let radiansBearing = atan2(y, x)
return radiansToDegrees(radians: radiansBearing)
}
func getNewTargetCoordinate(position: CLLocationCoordinate2D, userBearing: Float, distance: Float)-> CLLocationCoordinate2D{
let r = 6378140.0
let latitude1 = position.latitude * (Double.pi/180) // change to radiant
let longitude1 = position.longitude * (Double.pi/180)
let brng = Double(userBearing) * (Double.pi/180)
var latitude2 = asin(sin(latitude1)*cos(Double(distance)/r) + cos(latitude1)*sin(Double(distance)/r)*cos(brng));
var longitude2 = longitude1 + atan2(sin(brng)*sin(Double(distance)/r)*cos(latitude1),cos(Double(distance)/r)-sin(latitude1)*sin(latitude2));
latitude2 = latitude2 * (180/Double.pi)// change back to degree
longitude2 = longitude2 * (180/Double.pi)
// return target location
return CLLocationCoordinate2DMake(latitude2, longitude2)
}
}
结果:
推荐阅读
- python - 如何在ubuntu上的blender 2.80 python中安装熊猫?
- visual-studio - Visual Studio 包 - 获取当前活动的文档并对其进行编辑,包括 peek 定义
- asp.net-core - ASP.NET Core 将结果发布到电子邮件
- flutter - 颤动从firebase存储中删除图像
- r - ForestTools R 包中树顶检测的错误
- python - 在 numpy 数组上应用通用二元运算符
- aws-lambda - 从 lambda 调用 API 网关发送自定义标头导致 403 Forbidden
- c# - 如何在 C# .NET 中从 GitHub 下载原始文件
- python - HTML 刮板 Python 中的 TypeError。AttributeError:“NoneType”对象没有属性“get_text”
- r - 我可以在 Sankey Plot (networkD3::sankeyNetwork) 中旋转节点标签吗?