Skip to content

Instantly share code, notes, and snippets.

@alo9507
Last active July 30, 2019 01:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alo9507/236ae56e3e2cecd0cc8cf6a0e07af2d0 to your computer and use it in GitHub Desktop.
Save alo9507/236ae56e3e2cecd0cc8cf6a0e07af2d0 to your computer and use it in GitHub Desktop.
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