I am using Nativescript to Develop an IOS App
I am getting the magnetic Heading Degrees from the CLLocationManager
Location.service.ts
import * as geoLocation from "@nativescript/geolocation";
@Injectable()
export class LocationService {
private locationManager : CLLocationManager;
constructor() {
this.locationManager = CLLocationManager.alloc().init();
this.locationManager.desiredAccuracy = 3;
this.locationManager.distanceFilter = 0.1;
this.locationManager.headingFilter = 0.1;
}
getHeading(): number {
return this.locationManager?.heading?.magneticHeading;
}
convertDegreesToDirectionText(direction: number): string {
if (direction == null) {
return "N/A";
}
if (direction > 337.5) {
return "North";
}
if (direction <= 22.5 && direction >= 0) {
return "North";
}
if (direction > 22.5 && direction < 67.5) {
return "Northeast";
}
if (direction >= 67.5 && direction <= 112.5) {
return "East";
}
if (direction > 112.5 && direction < 157.5) {
return "Southeast";
}
if (direction >= 157.5 && direction <= 202.5) {
return "South";
}
if (direction > 202.5 && direction < 247.5) {
return "Southwest";
}
if (direction >= 247.5 && direction <= 292.5) {
return "West";
}
if (direction > 292.5 && direction < 337.5) {
return "Northwest";
}
return "Could not determine direction.";
}
startUpdatingLocation(): void {
this.locationManager.startUpdatingLocation();
}
stopUpdatingLocation(): void {
this.locationManager.stopUpdatingLocation();
}
startUpdatingHeading(): void {
this.locationManager.startUpdatingHeading();
}
stopUpdatingHeading(): void {
this.locationManager.stopUpdatingHeading();
}
}
Component.ts
import { Component, OnInit } from '@angular/core';
import { LocationService } from './location.service';
@Component({
selector: 'ns-details',
templateUrl: './item-detail.component.html',
})
export class ItemDetailComponent implements OnInit {
item: Item;
constructor(private locationService: LocationService) {
this.locationService.startUpdatingLocation();
this.locationService.startUpdatingHeading();
}
ngOnInit(): void {
console.log('Magnetic Heading',this.locationService.getHeading());
console.log('Magnetic Heading Text',this.locationService.convertDegreesToDirectionText(this.locationService.getHeading()));
}
}
How can i convert the magnetic heading degrees to true heading degrees ?
I do not want to use this.locationManager?.heading?.trueHeading; because true heading depends on the location services and gps to get the value and sometimes it returns -1 when it cannot determine the location, where as magnetic heading value always returns a value as it uses the device magnetometer.