Implement copassheading
All checks were successful
FarmMaps.Develop/FarmMapsLib/develop This commit looks good
All checks were successful
FarmMaps.Develop/FarmMapsLib/develop This commit looks good
This commit is contained in:
@@ -5,19 +5,43 @@ 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));
|
||||
};
|
||||
compassHeading(alpha, beta, gamma):number {
|
||||
|
||||
// Convert degrees to radians
|
||||
var alphaRad = alpha * (Math.PI / 180);
|
||||
var betaRad = beta * (Math.PI / 180);
|
||||
var gammaRad = gamma * (Math.PI / 180);
|
||||
|
||||
// Calculate equation components
|
||||
var cA = Math.cos(alphaRad);
|
||||
var sA = Math.sin(alphaRad);
|
||||
var cB = Math.cos(betaRad);
|
||||
var sB = Math.sin(betaRad);
|
||||
var cG = Math.cos(gammaRad);
|
||||
var sG = Math.sin(gammaRad);
|
||||
|
||||
// Calculate A, B, C rotation components
|
||||
var rA = - cA * sG - sA * sB * cG;
|
||||
var rB = - sA * sG + cA * sB * cG;
|
||||
var rC = - cB * cG;
|
||||
|
||||
// Calculate compass heading
|
||||
var compassHeading = Math.atan(rA / rB);
|
||||
|
||||
// Convert from half unit circle to whole unit circle
|
||||
if(rB < 0) {
|
||||
compassHeading += Math.PI;
|
||||
}else if(rA < 0) {
|
||||
compassHeading += 2 * Math.PI;
|
||||
}
|
||||
return compassHeading * (180/Math.PI);
|
||||
}
|
||||
|
||||
getCurrentCompassHeading(): Observable<number> {
|
||||
return Observable.create((observer: Observer<number>) => {
|
||||
window.addEventListener("deviceorientation", (event:DeviceOrientationEvent)=>{
|
||||
observer.next(this.compassHeading(event.alpha,event.beta,event.gamma));
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user