【问题标题】:Calculate bearing between two locations计算两个位置之间的方位角
【发布时间】:2015-04-03 12:01:34
【问题描述】:

我想计算 Swift 中两个位置之间的方位角。我尝试了一些代码,但它不起作用。我搜索了这个问题,但没有找到任何结果。

func calculat(userlocation:CLLocation){

    let latuserlocation: () = DegreesToRadians(userlocation.coordinate.latitude)

    let lonuserlocatioc: () = DegreesToRadians(userlocation.coordinate.longitude)


    latitude = NSString (string: places[activePlace]["lat"]!).doubleValue

    longitude = NSString (string: places[activePlace]["lon"]!).doubleValue


    let targetedPointLatitude: () = DegreesToRadians(latitude)

    let targetedPointlongitude: () = DegreesToRadians(longitude)


    let dlon = lonuserlocatioc  - targetedPointlongitude


    let y = sin(dLon) * cos(targetedPointLatitude);

    let x = cos(latuserlocation) * sin(targetedPointLatitude) - sin(latuserlocation) * cos(targetedPointLatitude) * cos(dLon);

    let radiansBearing = atan2(y, x);

return RadiansToDegrees(radiansBearing)

let dlon = lonuserlocatioc - targetedPointlongitude 的错误是:

(不能使用类型为“((),())”的参数列表调用“-”)

【问题讨论】:

  • 我尝试了这个但不起作用(在 Swift [重复] 中计算两个 CLLocation 点之间的方位)
  • 代码没有意义。有一个 return 语句,但 func 没有说它会返回一个值。为什么要将纬度和经度变量声明为元组,然后尝试对它们进行算术(减法)?与其说“不工作”,不如说确切的错误或问题是什么,因为有无数种方法可能会“不工作”。

标签: swift cllocation


【解决方案1】:

CLLocation Category for Calculating Bearing w/ Haversine function 相比,您的dlon 不同。这个答案有

let dlon = targetedPointlongitude - lonuserlocatioc

我不知道这是否是你的问题,但它看起来很奇怪。

【讨论】:

  • 我准确地从 javascript 代码转换此代码。奇点在哪里?如果你给我详细的信息,我可以编辑它。我检查了你的链接,对我来说看起来一样。也许我看不出区别。
  • 我给出的链接中的代码的 dlon 等于上述问题中代码的 -1 倍。
【解决方案2】:

这样的Swift函数;

func radians(n: Double) -> Double{
    return n * (M_PI / 180);
}

func degrees(n: Double) -> Double{
    return n * (180 / M_PI);
}

func logC(val:Double,forBase base:Double) -> Double {
    return log(val)/log(base);
}

func getBearing(startLat: Double,startLon:Double,endLat: Double,endLon: Double) -> Double{
    var s_LAT: Double , s_LON: Double, e_LAT: Double, e_LON: Double, d_LONG: Double, d_PHI: Double;

    s_LAT = startLat;
    s_LON = startLon;
    e_LAT = endLat;
    e_LON = endLon;

    d_LONG = e_LON - s_LON;

    d_PHI = logC(tan(e_LAT/2.0+M_PI/4.0)/tan(s_LAT/2.0+M_PI/4.0),forBase :M_E);
    if (abs(d_LONG)>M_PI){
        if(d_LONG>0.0){
            d_LONG = -(2.0 * M_PI - d_LONG);
        }else{
            d_LONG = (2.0 * M_PI - d_LONG);
        }
    }
    return degrees(atan2(d_LONG, d_PHI)+360.0)%360.0;
}

【讨论】:

  • @EricAya 好的,我将在几个小时后转换并编辑我的答案。很抱歉出现这种情况。
猜你喜欢
  • 2011-12-28
  • 1970-01-01
  • 1970-01-01
  • 2011-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-26
相关资源
最近更新 更多