2

I want to calculate bearing between two location in Swift. I tried some code but it do not work. I searched about this problem but I didn't find any result about this.

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)

The error on let dlon = lonuserlocatioc - targetedPointlongitude is:

(cannot invoke '-' with an argument list of type '((), ())')

  • possible duplicate of [Calculating bearing between two CLLocation points in Swift](http://stackoverflow.com/questions/26998029/calculating-bearing-between-two-cllocation-points-in-swift) – Eric Aya Apr 03 '15 at 12:16
  • i try this but not work (Calculating bearing between two CLLocation points in Swift [duplicate] ) – user3432650 Apr 03 '15 at 12:21
  • 1
    The code doesn't make sense. There's a return statement but func doesn't say it will return a value. Why would you declare latitude and longitude variables as tuples and then try to do arithmetic (subtraction) on them? Instead of saying "not work", you should say what the _exact_ error or problem was since there are countless ways in which things may "not work". –  Apr 03 '15 at 12:26

2 Answers2

0

Compared to CLLocation Category for Calculating Bearing w/ Haversine function, your dlon is different. That answer has

let dlon = targetedPointlongitude - lonuserlocatioc

I don't know if that's your problem but it looks odd.

Community
  • 1
  • 1
emrys57
  • 6,679
  • 3
  • 39
  • 49
  • Accualy i convert this code from javascript code. Where is the odd point ? If you give me detailed information i can edit it. I inspect your link, it's looks like same for me. Maybe i can't see the difference. – Emre Erol Sep 07 '16 at 07:35
  • The code at the link I gave has dlon equal to -1 times the code in the question above. – emrys57 Sep 07 '16 at 08:59
-2

Swift function like this;

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;
}
Emre Erol
  • 470
  • 1
  • 7
  • 25