Ioniconf 2022 logo

Uniting our global community of Ionic developers and contributors. May 25, 2022.

DocsPluginsCLI
pwaiosandroid

Motion

The Motion API tracks accelerometer and device orientation (compass heading, etc.)

Permissions

This plugin is currently implemented using Web APIs. Most browsers require permission before using this API. To request permission, prompt the user for permission on any user-initiated action (such as a button click):

myButton.addEventListener('click', async () => {
  try {
    await DeviceMotionEvent.requestPermission();
  } catch (e) {
    // Handle error
    return;
  }

  // Once the user approves, can start listening:
  const { Motion } = Capacitor.Plugins;
  Capacitor.Plugins.Motion.addListener('accel', event => {});
});

Example

const { Motion } = Capacitor.Plugins;
Motion.addListener('accel', event => {});

See the DeviceMotionEvent API to understand the data supplied in event.

API

addListener(…)

addListener(eventName: 'accel', listenerFunc: (event: MotionEventResult) => void) => PluginListenerHandle

Listen for accelerometer data

Param Type
eventName "accel"
listenerFunc (event: MotionEventResult) => void

Returns: PluginListenerHandle


addListener(…)

addListener(eventName: 'orientation', listenerFunc: (event: MotionOrientationEventResult) => void) => PluginListenerHandle

Listen for device orientation change (compass heading, etc.)

Param Type
eventName "orientation"
listenerFunc (event: MotionOrientationEventResult) => void

Returns: PluginListenerHandle


removeAllListeners()

removeAllListeners() => void

Remove all native listeners for this plugin


Interfaces

PluginListenerHandle

Prop Type
remove () => void

MotionEventResult

Prop Type
acceleration { x: number; y: number; z: number; }
accelerationIncludingGravity { x: number; y: number; z: number; }
rotationRate { alpha: number; beta: number; gamma: number; }
interval number

MotionOrientationEventResult

Prop Type
alpha number
beta number
gamma number
Previous
<- Modals
Next
Network ->
Contribute ->