菜鸟教程小白 发表于 2022-12-12 10:48:33

iOS CoreMotion - 计算高度差


                                            <p><p>是否可以使用 CoreMotion 中的数据计算 iPhone 移动时的高度差?</p>

<p>例如,保存 iPhone 的初始位置,然后移动 iPhone,从而产生一个新位置。我有兴趣检测 iPhone 的高度是否从其初始位置移动。我不需要绝对单位,只需要一个相对值来指示高度与原始高度相比变化的百分比。</p>

<pre><code>let manager = CMMotionManager()

// Save initial attitude
var initialAttitude = manager.deviceMotion.attitude

if manager.deviceMotionAvailable {
manager.startDeviceMotionUpdatesToQueue(NSOperationQueue.mainQueue()) {
    (data: CMDeviceMotion!, error: NSError!) in

    // Get the difference between the initial and new attitude
    data.attitude.multiplyByInverseOfAttitude(initialAttitude)

    // Is there a way to calculate the relative change in height?

    }
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>在较新的 iPhone 上,确实可以从随附的气压计(适用于 iphone 6)获得<em>相对高度</em>。 Core Motion 已被扩展以适应这一点。</p>

<p><code>CMAltimeter</code> - 提供高度变化事件的传递。</p>

<p><code>CMAltitudeData</code> - 封装高度变化事件(以米级分辨率)。</p>

<p>这种东西应该可以的......</p>

<pre><code> if CMAltimeter.isRelativeAltitudeAvailable() {
   altimeter.startRelativeAltitudeUpdatesToQueue(altimeterQueue,
   withHandler:
         { (data:CMAltitudeData?,
             error: NSError?) in
             if let altitudeData = data {
               self.timeStamp = altitudeData.timestamp
               self.relativeAltitude = altitudeData.relativeAltitude
             }
         }
   )
}
</code></pre>

<p>我们没有得到绝对高度,只有相对高度变化。初始高度被认为是 0。因此您无法真正获得百分比变化(没有任何意义),但您可以获得米级精度的绝对变化。</p></p>
                                   
                                                <p style="font-size: 20px;">关于iOS CoreMotion - 计算高度差,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/36038491/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/36038491/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iOS CoreMotion - 计算高度差