【问题标题】:Converting two coordinate points to distance in west-east-facing and north-south-facing direction将两个坐标点转换为东西向和南北向的距离
【发布时间】:2019-07-05 07:16:57
【问题描述】:

我正在尝试找出如何在给定两点坐标(经纬度)的情况下分别计算两点之间的“东西向”距离和“南北向”距离,为了找出该线与北方方向不同的程度。 In short I need to calculate x and y in meters in order to get the degree of alpha, when x and y are given by longitude and latitude.

我知道CLLocation有一个计算两点距离的函数:

距离(从位置:CLLocation)-> CLLocationDistance

我试图打开该函数的源代码以了解如何分离这两个组件,但我没有找到如何打开该代码。

【问题讨论】:

    标签: ios core-location cllocationmanager cllocation


    【解决方案1】:

    你可以用这个

    func getDistanceAndAngle(positionA: CLLocation, positionB: CLLocation) -> (Float, Float, Float){
    
        let distanceInMeters = Float(positionA.distance(from: positionB)) // result is in meters
        print(distanceInMeters)
        //search for the degree
        let angle = bearingFromLocation(fromLocation: positionA.coordinate, toLocation: positionB.coordinate)
        print("ANGLE", angle)
        let xDistance = abs(distanceInMeters * cos(DegreesToRadians(degrees: angle)))
        let yDistance = abs(distanceInMeters * sin(DegreesToRadians(degrees: angle)))
    
        return (xDistance,yDistance,angle)
    }
    
    
    func bearingFromLocation(fromLocation:CLLocationCoordinate2D, toLocation: CLLocationCoordinate2D)-> Float{
    
        let lat1 = DegreesToRadians(degrees: Float(fromLocation.latitude))
        let lon1 = DegreesToRadians(degrees: Float(fromLocation.longitude))
    
        let lat2 = DegreesToRadians(degrees: Float(toLocation.latitude))
        let lon2 = DegreesToRadians(degrees: Float(toLocation.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)
        print("radian", radiansBearing)
        let degreesBearing = RadiansToDegrees(radians: radiansBearing)
        print("deg", degreesBearing)
    
        if (degreesBearing >= 0) {
            return degreesBearing
        } else {
            return degreesBearing + 360.0
        }
    }
    
    func DegreesToRadians(degrees: Float)->Float {return degrees * Float.pi / 180.0}
    func RadiansToDegrees(radians: Float)->Float {return radians * 180.0/Float.pi}
    

    注意事项: 角度是从北到东 所以北是0度,东是90度。 X 和 Y 始终为正。所以如果你想让它向左和向下负数,你可以尝试使用 degree 使其向右。

    【讨论】:

    • 我需要能够计算角度,而不管第二个坐标放置在哪里(即它可以放置在每个象限中),我不确定我理解你所说的“放使用度数使其正确”
    • 是的,它可以计算每个象限。对于 0 到 90 度,x 和 y 为正 对于 90 到 180 度,x 为正,y 为负。对于 180 到 270 度,两者都是负数。对于 270 到 360 度,x 为负,y 为正。我的意思是我故意使 x 和 y 为正,因为距离不能为负。如果要确定西、南为负,可以用度数加负号。
    【解决方案2】:

    您可以将 point1 和 point2 转换为 MKMapPoint。它为您提供在 2D 平面上投影的坐标。然后你可以很容易地得到 x 和 y 的差异。使用简单的数学,你可以计算出 alpha 值。您可以使用 CLLocationCoordinate2D 初始化 MKMapPoint。

    https://developer.apple.com/documentation/mapkit/mkmappoint

    【讨论】:

      【解决方案3】:

      假设我们有:

      let locationA = CLLocation(..)
      let locationB = CLLocation(..)
      

      我可能仍想使用 Apple 提供的功能并创建仅在纬度或经度上不同的新位置。比如:

      let latitudeA = CLLocation(latitude: locationA.latitude, longitude: locationA.longitude)
      // notice I use latitude from B but KEEP longitude from A to keep them parallel)
      let latitudeB = CLLocation(latitude: locationB.latitude, longitude: locationA.longitude)
      let northSouthDistance = latitudeA.distance(from: latitudeB)
      

      等

      【讨论】:

      • 这似乎是最简单的方法,但是我不确定如何处理“负坐标”以便以后计算正确的角度,因为我假设距离(来自: )-方法返回距离的绝对值。假设我们有一个以 locationA 为原点的坐标系,locationB 应该能够放置在每个象限中,而不仅仅是在第一个象限中。
      • 公平,我尝试在堆栈溢出中查找类似于“计算两个 CLLocations 之间的角度”的内容,例如:stackoverflow.com/questions/3925942/…
      • 哦,我看到接受的答案只包括这个,请注意,当两极像南北切换时,这个概率会中断。因为它会从 +180 度变为 -179 度,这实际上应该只是一个度数的变化。
      猜你喜欢
      • 2011-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多