Prashant Tukadiya
Prashant Tukadiya

Reputation: 16416

Convert degree to iOS degree system

So I am trying to find bearing from two location coordinates using

https://www.sunearthtools.com/tools/distance.php.

input

Coordinate A : 21.642534, 69.607003, Coordinate B : 21.642083, 69.614587

Output is 93.66 Degrees

enter image description here

Now I want to show this on circle So I am using following code

    let view = UIView(frame: self.view.bounds)
    self.view.addSubview(view)


    let circle = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.width - 20, height: view.frame.width - 20))
    view.addSubview(circle)
    circle.center = view.center
    circle.layer.cornerRadius = circle.frame.height / 2
    circle.backgroundColor = UIColor.gray.withAlphaComponent(0.7)




    let dotView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 10))
    dotView.layer.cornerRadius = dotView.frame.width / 2
    dotView.backgroundColor = UIColor.purple.withAlphaComponent(0.89)
    dotView.center = circle.center
    circle.addSubview(dotView)


    let location1 = CLLocation(latitude: 21.642534, longitude: 69.607003)
    let location2 = CLLocation(latitude: 21.642083, longitude: 69.614587)

    let angleStep = CGFloat(location1.bearingRadianTo(location: location2))
    print(location1.bearingDegreesTo(location: location2))
    let xPos = cos(angleStep) * (circle.frame.width / 2)
    let yPos = sin(angleStep) * (circle.frame.width / 2)

    dotView.center = CGPoint(x:circle.center.x + xPos - circle.frame.origin.x, y:circle.center.y + yPos - circle.frame.origin.y);

Output is purple dot is at bottom it should be at right side Just like first image.

I have verified that I am getting correct value in angleStep

After some google I found that for iOS we have different degree system

enter image description here

enter image description here

How Can I Convert degree system to iOS degree system so I got the purple circle at right side ?

Thanks in advance

Upvotes: 4

Views: 434

Answers (1)

rmaddy
rmaddy

Reputation: 318774

Real world headings use 0 degree for north (up). But as seen in your last diagram, 0 degrees is to the right, not to the top in the iOS coordinate system. All you need to do is subtract π/2 radians from angleStep to convert from "real" heading to "iOS" heading.

let angleStep = CGFloat(location1.bearingRadianTo(location: location2)) - CGFloat.pi / 2

Upvotes: 2

Related Questions