计算两个位置之间的方位

时间:2015-04-03 12:01:34

标签: swift cllocation

我想计算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上的错误是:

  

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

2 个答案:

答案 0 :(得分:0)

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

let dlon = targetedPointlongitude - lonuserlocatioc

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

答案 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;
}