25 lines
619 B
TypeScript
25 lines
619 B
TypeScript
|
import { Injectable } from '@angular/core';
|
||
|
import { Observer, Observable } from 'rxjs';
|
||
|
|
||
|
|
||
|
@Injectable()
|
||
|
export class DeviceOrientationService {
|
||
|
|
||
|
/**
|
||
|
* Tries HTML5 geolocation.
|
||
|
*
|
||
|
* Wraps the Geolocation API into an observable.
|
||
|
*
|
||
|
* @return An observable of Bearing
|
||
|
*/
|
||
|
getCurrentBearing(): Observable<number> {
|
||
|
return Observable.create((observer: Observer<number>) => {
|
||
|
let sensor = new Magnetometer();
|
||
|
sensor.onreading= (ev:Event) => {
|
||
|
observer.next(Math.atan2(sensor.y, sensor.x) * (180 / Math.PI));
|
||
|
};
|
||
|
});
|
||
|
}
|
||
|
|
||
|
}
|