Skip to main content

Device eMagnetometer

Requires Cordova plugin: cordova-plugin-magnetometer. For more info, please see the Device Orientation docs.

https://github.com/sdesalas/cordova-plugin-magnetometer

Stuck on a Cordova issue?

Don't waste precious time on plugin issues.

If you're building a serious project, you can't afford to spend hours troubleshooting. Ionic’s experts offer premium advisory services for both community plugins and premier plugins.

Installation#

$ npm install cordova-plugin-magnetometer $ npm install @ionic-native/magnetometer $ ionic cap sync

Supported Platforms#

  • Android
  • iOS

Usage#

React#

Learn more about using Ionic Native components in React

Angular#

// DeviceOrientationCompassHeading is an interface for compassimport { Magnetometer, MagnetometerReading } from '@ionic-native/device-orientation/ngx';
constructor(private magnetometer: Magnetometer) { }
...
// Get the device current compass headingthis.deviceOrientation.getReading().then(  (data: MagnetometerReading) => console.log(data),  (error: any) => console.log(error));
// Watch the device compass heading changevar subscription = this.deviceOrientation.watchReadings().subscribe(  (data: MagnetometerReading) => console.log(data));
// Stop watching heading changesubscription.unsubscribe();