Device eMagnetometer

Contents

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?

Installation

Community Enterprise
ionic cordova plugin add cordova-plugin-magnetometer npm install @ionic-native/magnetometer
Ionic EE comes with fully supported and maintained plugins from the Ionic Team. Learn More   or   Contact Us
ionic enterprise register --key=YOURPRODUCTKEY npm install @ionic-enterprise/magnetometer

Supported Platforms

  • Android
  • iOS

Usage

// DeviceOrientationCompassHeading is an interface for compass
import { Magnetometer, MagnetometerReading } from '@ionic-native/device-orientation/ngx';

constructor(private magnetometer: Magnetometer) { }

...

// Get the device current compass heading
this.deviceOrientation.getReading().then(
  (data: MagnetometerReading) => console.log(data),
  (error: any) => console.log(error)
);

// Watch the device compass heading change
var subscription = this.deviceOrientation.watchReadings().subscribe(
  (data: MagnetometerReading) => console.log(data)
);

// Stop watching heading change
subscription.unsubscribe();