在 Ionic 3 中访问设备运动



因为cordova-plugin-device-motion插件现已弃用,所以我正在尝试在我的 Ionic 3 应用程序中使用 W3C 加速度计 API:

// At the top of my TS file:
declare var Accelerometer;
// In my component:
let sensor = new Accelerometer();
sensor.start();
sensor.onreading = () => {
console.log("Acceleration along X-axis: " + sensor.x);
console.log("Acceleration along Y-axis: " + sensor.y);
console.log("Acceleration along Z-axis: " + sensor.z);
}
sensor.onerror = event => console.log(event.error.name, event.error.message);

这在 Chrome 中进行出色的测试。

但是,当我尝试使用 iOS 平台在真实设备 (iPhone 6( 上运行它时4.5.5,我收到一个错误:Runtime Error: Can't find variable: Accelerometer.

我在这里做错了什么?有没有人对在 Ionic 3 应用程序中获取 W3C 设备运动数据有任何见解?如果cordova-plugin-device-motion被弃用,我应该在 Ionic 中使用什么?

根据CanIUse的说法,它目前只是一个Chrome功能。 您可能希望签出 devicemotion 事件 DeviceMotion

最新更新