# Capacitor - Using Motion

**URL:** <https://forum.ionicframework.com/t/capacitor-using-motion/168369>\
**Category:** Capacitor\
**Created:** [July 10, 2019, 10:19pm UTC](https://forum.ionicframework.com/t/capacitor-using-motion/168369 "2019-07-10T22:19:08Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![SpookieUkie](https://avatars.discourse-cdn.com/v4/letter/s/3da27b/32.png) [@SpookieUkie](https://forum.ionicframework.com/u/SpookieUkie)\
**Post date:** [July 10, 2019, 10:19pm UTC](https://forum.ionicframework.com/t/capacitor-using-motion/168369/1 "2019-07-10T22:19:08Z")

</div>

I am confused on how to use the motion api plugin with Capacitor. I am trying to get the ‘compass heading’ but I can’t seems to get it. When I listen to an Orientation and get a MotionOrientationEventResult, the only event properties are alpha beta and gamma. Here is the sample of what I have.

import { Plugins, MotionOrientationEventResult, MotionEventResult } from ‘@capacitor/core’;  
const { Geolocation } = Plugins;  
const { Motion } = Plugins;

private getMotion() {  
// try {  
Motion.addListener(‘accel’, (event: MotionEventResult) =\> {  
console.log (‘accel’);  
console.log (event);

```
})

Motion.addListener('orientation', (event: MotionOrientationEventResult) => {
  console.log ('Orientation');
  console.log (event);
});

```

}

Any help on this would be great.

Thanks

---

<div class="post-metadata">

**Author:** ![ChrisGriiffith](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/chrisgriiffith/32/66576_2.png) [@ChrisGriiffith](https://forum.ionicframework.com/u/ChrisGriiffith)\
**Post date:** [July 10, 2019, 10:45pm UTC](https://forum.ionicframework.com/t/capacitor-using-motion/168369/2 "2019-07-10T22:45:55Z")

</div>

What device are you testing it on?

---

<div class="post-metadata">

**Author:** ![SpookieUkie](https://avatars.discourse-cdn.com/v4/letter/s/3da27b/32.png) [@SpookieUkie](https://forum.ionicframework.com/u/SpookieUkie)\
**Post date:** [July 10, 2019, 10:47pm UTC](https://forum.ionicframework.com/t/capacitor-using-motion/168369/3 "2019-07-10T22:47:41Z")

</div>

The latest iPad Pro, 11" Model with iOS 12.3.1 and and iPhone 6. I am using Ionic 4.5.0 with Angular 7.2.15 and Capacitor 1.0.0

I am using Visual Studio Code and the code intelligence for that event MotionOrientationEventResult only gives me the properties, alpha, beta and gamma

---

<div class="post-metadata">

**Author:** ![ChrisGriiffith](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/chrisgriiffith/32/66576_2.png) [@ChrisGriiffith](https://forum.ionicframework.com/u/ChrisGriiffith)\
**Post date:** [July 10, 2019, 11:03pm UTC](https://forum.ionicframework.com/t/capacitor-using-motion/168369/4 "2019-07-10T23:03:46Z")

</div>

See this code snippet: [https://stackoverflow.com/questions/18112729/calculate-compass-heading-from-deviceorientation-event-api](https://stackoverflow.com/questions/18112729/calculate-compass-heading-from-deviceorientation-event-api)

I would also look at: [https://github.com/lamplightdev/compass](https://github.com/lamplightdev/compass)

---

<div class="post-metadata">

**Author:** ![SpookieUkie](https://avatars.discourse-cdn.com/v4/letter/s/3da27b/32.png) [@SpookieUkie](https://forum.ionicframework.com/u/SpookieUkie)\
**Post date:** [July 10, 2019, 11:13pm UTC](https://forum.ionicframework.com/t/capacitor-using-motion/168369/5 "2019-07-10T23:13:29Z")

</div>

Oh cool, I will check those links out. Thanks

---

<div class="post-metadata">

**Author:** ![SpookieUkie](https://avatars.discourse-cdn.com/v4/letter/s/3da27b/32.png) [@SpookieUkie](https://forum.ionicframework.com/u/SpookieUkie)\
**Post date:** [July 11, 2019, 4:16pm UTC](https://forum.ionicframework.com/t/capacitor-using-motion/168369/6 "2019-07-11T16:16:24Z")

</div>

I still seem to get getting inaccurate values. I am taking the alpha, beta and gamma values and plugging them into the function mentioned in the post, but when I compare to a compass app, it is nowhere close. The gamma value is always negative as well. Not sure if that is an issue. Its like the device is not calibrated.

Any thoughts? Thanks

```auto
Motion.addListener('orientation', (event: MotionOrientationEventResult) => {
    orientation.degrees = this.compassHeading(event.alpha, event.beta, event.gamma);
}

```

```auto
private compassHeading(alpha, beta, gamma) {

    // Convert degrees to radians
    var alphaRad = alpha * (Math.PI / 180);
    var betaRad = beta * (Math.PI / 180);
    var gammaRad = gamma * (Math.PI / 180);
  
    // Calculate equation components
    var cA = Math.cos(alphaRad);
    var sA = Math.sin(alphaRad);
    var cB = Math.cos(betaRad);
    var sB = Math.sin(betaRad);
    var cG = Math.cos(gammaRad);
    var sG = Math.sin(gammaRad);
  
    // Calculate A, B, C rotation components
    var rA = - cA * sG - sA * sB * cG;
    var rB = - sA * sG + cA * sB * cG;
    var rC = - cB * cG;
  
    // Calculate compass heading
    var compassHeading = Math.atan(rA / rB);
  
    // Convert from half unit circle to whole unit circle
    if(rB < 0) {
      compassHeading += Math.PI;
    }else if(rA < 0) {
      compassHeading += 2 * Math.PI;
    }

    // Convert radians to degrees
    compassHeading *= 180 / Math.PI;

  
    return compassHeading;
  
  }

```

---

<div class="post-metadata">

**Author:** ![SpookieUkie](https://avatars.discourse-cdn.com/v4/letter/s/3da27b/32.png) [@SpookieUkie](https://forum.ionicframework.com/u/SpookieUkie)\
**Post date:** [July 11, 2019, 4:56pm UTC](https://forum.ionicframework.com/t/capacitor-using-motion/168369/7 "2019-07-11T16:56:40Z")

</div>

Another update, it really does seem to be a calibration issue as I get different values each time I load the app standing in the same direction. The goal I am trying to reach is that when I take a picture, I want to state what direction I am facing. Does anyone have thoughts on how that can be accomplished?

Thanks

---

<div class="post-metadata">

**Author:** ![yvesbouvier](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/yvesbouvier/32/130896_2.png) [@yvesbouvier](https://forum.ionicframework.com/u/yvesbouvier)\
**Post date:** [February 29, 2020, 8:11am UTC](https://forum.ionicframework.com/t/capacitor-using-motion/168369/8 "2020-02-29T08:11:54Z")

</div>

Hey, did you ever figure this one out ? It seems like it is setting the orientation the app is open at at 0 and then change the number relative to that first position. Not sure how to get the true or magnet North.

---

<div class="post-metadata">

**Author:** ![edwinofsakh](https://avatars.discourse-cdn.com/v4/letter/e/ecb155/32.png) [@edwinofsakh](https://forum.ionicframework.com/u/edwinofsakh)\
**Post date:** [May 17, 2020, 9:03pm UTC](https://forum.ionicframework.com/t/capacitor-using-motion/168369/9 "2020-05-17T21:03:01Z")

</div>

If it happens just on iOS you can try to use `webkitCompassHeading` instead of `alpha`. Check WebKit JS documentation for more details.
