Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
extension MapViewController: SensorDispatchHandler {
/*
After initializing a WearableDeviceSession and calling sensorDispatch.handler = self in viewDidLoad(),
Your view controllers can receive raw heading information with the receivedRotation delegate method.
We use this information to render a custom vision cone on screen.
*/
func receivedRotation(quaternion: Quaternion, accuracy: QuaternionAccuracy, timestamp: SensorTimestamp) {
// 1. Convert raw heading data to degrees
let qMap = Quaternion(ix: 1, iy: 0, iz: 0, r: 0)
let qResult = quaternion * qMap
let yaw: Double = (-qResult.zRotation).toDegrees()
// 2. Get user location from the Mapbox MapView
guard let userLocation = mapView.userLocation?.location, userLocation.horizontalAccuracy > 0 else {
return
}
// 3. Convert degrees to magnetic degreees (0°-360°)
let magneticDegrees: Double = (yaw < 0) ? 360 + yaw : yaw
// 4. Update the direction of a custom vision cone to show user heading in the UI
vision.updateVisionPath(center: userLocation.coordinate, orientation: 360 - magneticDegrees)
locationManager.visionPolygon(for: userLocation.coordinate, orientation: 360 - magneticDegrees)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.