简体   繁体   中英

Accessing device motion in Ionic 3

Because the cordova-plugin-device-motion plugin is now deprecated , I'm trying to use the W3C Accelerometer API in my Ionic 3 app:

// 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);

This works great testing in Chrome.

However, when I try running this on a real device (iPhone 6) using iOS platform 4.5.5 , I get an error: Runtime Error: Can't find variable: Accelerometer .

What am I doing wrong here? Does anyone have any insight into getting W3C device motion data in an Ionic 3 app? If cordova-plugin-device-motion is deprecated, what should I be using in Ionic?

According to CanIUse it's only a Chrome feature for now. You might want to checkout the devicemotion event DeviceMotion

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