首页 > 解决方案 > 如何从CoreLocation中的两个坐标获得对称坐标

问题描述

我有两个CLLocationCoordinate2D坐标:coord1coord2。我想用作中心在 Swiftcoord1中获得对称坐标。coord3有没有办法计算?

在此处输入图像描述

标签: swiftcore-locationcllocationcoordinate2d

解决方案


用法:

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)
   }
}

结果

在此处输入图像描述


推荐阅读