如何从CoreLocation中的两个坐标获得对称坐标

Posted

技术标签:

【中文标题】如何从CoreLocation中的两个坐标获得对称坐标【英文标题】:How to get a symmetric coordinate from two coordinates in CoreLocation 【发布时间】:2021-07-28 09:47:20 【问题描述】:

我有两个CLLocationCoordinate2D 坐标:coord1coord2。我想以coord1 为中心在Swift 中获得对称坐标coord3。有没有办法计算?

【问题讨论】:

【参考方案1】:

用法:

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://***.com/a/52831007/6576315 https://***.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)
   

结果

【讨论】:

以上是关于如何从CoreLocation中的两个坐标获得对称坐标的主要内容,如果未能解决你的问题,请参考以下文章

如何获得高于 1Hz 的 CoreLocation 刷新率?

如何使用 CoreLocation 框架而不是谷歌地图 API 获取任何给定地址的坐标?

如何在 iPhone 模拟器中禁用 coreLocation?

CoreLocation iOS9 存储坐标

需要一种从 iPhone CoreLocation 上的地址获取 GPS 坐标的方法

如何获得两个地理点坐标之间的最短行驶路径和距离?