I am trying to get the direction in my nativescript app using the CLHeading trueHeading but it always returns as null.
locationservice.ts
import { Injectable } from "@angular/core";
@Injectable()
export class LocationService {
private iosLocManager: CLLocationManager;
private locManagerDelegate: LocationMangerDelegate;
constructor() {
this.iosLocManager = new CLLocationManager();
this.locManagerDelegate = new LocationMangerDelegate();
this.iosLocManager.desiredAccuracy = 3;
this.iosLocManager.distanceFilter = 0.1;
this.iosLocManager.delegate = this.locManagerDelegate;
}
getDirection(): number {
return this.locManagerDelegate.currentHeading;
}
startUpdatingHeading(): void {
this.locManagerDelegate.currentHeading = null;
this.iosLocManager.startUpdatingHeading();
}
stopUpdatingHeading(): void {
this.iosLocManager.stopUpdatingHeading();
}
}
export class LocationMangerDelegate extends NSObject implements CLLocationManagerDelegate {
public static ObjCProtocols = [CLLocationManagerDelegate];
currentHeading: number;
locationManagerDidUpdateHeading(locationManager: CLLocationManager, heading: CLHeading): void {
this.currentHeading = heading.trueHeading;
}
}
Component
constructor(private locationService: LocationService) {
this.locationService.startUpdatingHeading();
}
GetDirection(): void {
let direction = this.locationService.getDirection();
}
how can I get the value of trueHeading or how can I get the direction in NativeScript IOS ?
Package.json
{
"dependencies": {
"@angular/animations": "~13.2.0",
"@angular/common": "~13.2.0",
"@angular/compiler": "~13.2.0",
"@angular/core": "~13.2.0",
"@angular/forms": "~13.2.0",
"@angular/http": "7.2.16",
"@angular/platform-browser": "~13.2.0",
"@angular/platform-browser-dynamic": "~13.2.0",
"@angular/router": "~13.2.0",
"base-64": "^0.1.0",
"cross-env": "^5.2.0",
"lodash": "^4.17.11",
"@nativescript/angular": "^13.0.0",
"@nativescript/appversion": "2.0.0",
"@nativescript/camera": "5.0.10",
"nativescript-couchbase": "^1.0.18",
"@nativescript/email": "2.0.5",
"@nativescript/geolocation": "8.0.2",
"nativescript-phone": "3.0.3",
"nativescript-screen-orientation": "^2.0.0",
"nativescript-theme-core": "~1.0.4",
"reflect-metadata": "~0.1.13",
"rxjs": "~7.5.0",
"rxjs-compat": "^6.4.0",
"utf8": "^3.0.0",
"zone.js": "~0.11.5",
"@nativescript/core": "~8.2.0"
},
"devDependencies": {
"@angular/compiler-cli": "~13.2.0",
"@nativescript/schematics": "~0.5.0",
"@ngtools/webpack": "~13.2.0",
"@nativescript/webpack": "5.0.6",
"@angular-devkit/build-angular": "~13.2.0",
"@nativescript/ios": "8.2.1",
"@nativescript/types": "8.1.1",
"typescript": "~4.5.5"
},
"readme": "NativeScript Application",
"main": "./src/main.ts"
}
Reference.d.ts
/// <reference path="./node_modules/@nativescript/types/index.d.ts" />
Info.plist
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>magnetometer</string>
<string>gps</string>
<string>location-services</string>
</array>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Description</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Description</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Description</string>
How can i get the direction from the CLHeading trueHeading? . it always returns null ? what am I missing?
Check the docs:
This property contains a valid value only if location updates are also enabled for the corresponding location manager object. Because the position of true north is different from the position of magnetic north on the Earth’s surface, Core Location needs the current location of the device to compute the value of this property.