Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(magnetometer): add new plugin #2917

Merged
merged 1 commit into from
Feb 14, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions src/@ionic-native/plugins/magnetometer/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { Injectable } from '@angular/core';
import { Cordova, IonicNativePlugin, Plugin } from '@ionic-native/core';
import { Observable } from 'rxjs';

export interface MagnetometerReading {
/**
* X reading of magnetometer. (Number)
*/
x: number;

/**
* Y reading of magnetometer. (Number)
*/
y: number;

/**
* Z reading of magnetometer. (Number)
*/
z: number;

/**
* Calculated total - always positive of magnetometer. (Number)
*/
magnitude: number;

}

/**
* @name Device eMagnetometer
* @description
* Requires Cordova plugin: `cordova-plugin-magnetometer`. For more info, please see the [Device Orientation docs](https://github.com/sdesalas/cordova-plugin-magnetometer).
*
* @usage
* ```typescript
* // 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();
* ```
* @interfaces
* MagnetometerReading
*/
@Plugin({
pluginName: 'Magnetometer',
plugin: 'cordova-plugin-magnetometer',
pluginRef: 'cordova.plugins.magnetometer',
repo: 'https://github.com/sdesalas/cordova-plugin-magnetometer',
platforms: [
'Android',
'iOS',
]
})
@Injectable()
export class Magnetometer extends IonicNativePlugin {
/**
* Get the current compass reading.
* @returns {Promise<MagnetometerReading>}
*/
@Cordova()
getReading(): Promise<MagnetometerReading> {
return;
}

/**
* Get the device current heading at a regular interval
*
* Stop the watch by unsubscribing from the observable
* @param {DeviceOrientationCompassOptions} [options] Options for compass. Frequency and Filter. Optional
* @returns {Observable<DeviceOrientationCompassHeading>} Returns an observable that contains the compass heading
*/
@Cordova({
callbackOrder: 'reverse',
observable: true,
clearFunction: 'stop'
})
watchReadings(
): Observable<MagnetometerReading> {
return;
}
}