简体   繁体   中英

How to determine device location using CoreMotion framework?

For instance: app is runs and we initialized the start coordinates ( x=0, y=0, z=0 ) and coordinate system is equal to device coordinate system at startup and then it must be fixed ie not attached to device. I want to determine x, y, z in each moment when event of MotionManager fired like this:

motionManager.startDeviceMotionUpdates(to: OperationQueue.current!) { data, error in
    x = ...
    y = ...
    z = ...
}

But i don't know how to do this.

Thanks in advance!

Use it like:

motionManger.startDeviceMotionUpdates(to: OperationQueue.current!) { (deviceMotion, deviceMotionError) in
            self.handleDeviceMotionUpdates(deviceMotion!)
}

func handleDeviceMotionUpdates(_ deviceMotion: CMDeviceMotion){

   let acceleration = deviceMotion.gravity
   print("Acceleration X>\(acceleration.x) Y>\(acceleration.y) Z>\(acceleration.z)\n")

   let userAcceleration = deviceMotion.userAcceleration
   print("User Acceleration X>\(userAcceleration.x) Y>\(userAcceleration.y) Z>\(userAcceleration.z)\n")
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM