Finding user's bearing from current location in 2D plane iOS

Viewed 184

I am trying to access user's bearing from current location in my iOS app.

I am using below code to access it

func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
    heading = newHeading.trueHeading > 0 ? newHeading.trueHeading : newHeading.magneticHeading
}

However, it's not useful if we look at in 2D plane, where degree ranges from 0 to 360 clockwise. I want to send the bearing data to a different subscriber user through socket where an annotation with heading indicator(just like current location indicator with heading) on map can be displayed on iOS and Android. I am using a custom image which has heading indicator pointing up(12 O'Clock) to show on it on my other app.

Android has bearing which is the kind value I am looking for.

How can I get that value?

Thanks

1 Answers

Heading is determined by the magnetometer and indeed can't be used. It's a static value.

Bearing is determined by comparing subsequent (GPS) locations. It's a dynamic value over time.

iOS supplies bearing but names it 'course' with: locationManager(_:didUpdateLocations:).

You must read CLLocation's course property.

Because course can only be determined if the mobile device is moving, the course accuracy depends on speed and GPS accuracy. You must therefore use courseAccuracy to determine how/if you'll show this on the UI.

Related