FarmMapsLib/projects/common-map/src/fm-map/services/device-orientation.service.ts

25 lines
619 B
TypeScript
Raw Normal View History

2019-11-25 13:34:51 +00:00
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));
};
});
}
}