-1

I am trying to get the heading value in nativescript iOS application using CLLocationManager, CLLocationManagerDelegate.

LocationService.ts

import { Injectable } from "@angular/core";

@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;
        this.locationManager.delegate = new LocationMangerDelegate();
        this.locationManager.requestWhenInUseAuthorization();
        this.locationManager.requestAlwaysAuthorization();
    }

    requestLocation(): void{
        this.locationManager.requestLocation();
    }

    startUpdatingLocation(): void {
        this.locationManager.startUpdatingLocation();
      }

      startUpdatingHeading(): void {
        this.locationManager.startUpdatingHeading();
      }
} 

class LocationMangerDelegate extends NSObject implements CLLocationManagerDelegate {
    public static ObjCProtocols = [CLLocationManagerDelegate];

    locationManagerDidUpdateLocations(manager: CLLocationManager, locations: NSArray<CLLocation> | CLLocation[]): void{
        console.log('we are in');
    };


    public locationManagerDidFailWithError(manager: CLLocationManager, error: NSError): void{
        console.log('error');
    };

    public locationManagerDidUpdateHeading(manager: CLLocationManager, newHeading: CLHeading): void{
        console.log('updated heading');
    };

    public locationManagerShouldDisplayHeadingCalibration(manager: CLLocationManager): boolean{
        console.log('header calibration');
        return true;
    };
}

TestComponent.ts

import { LocationService } from "../shared/location.service";

export class TestComponent implements OnInit{
    

    constructor(
        private locationService: LocationService) {
        this.locationService.startUpdatingHeading();
    }

Info.plist

<key>UIRequiredDeviceCapabilities</key>
        <array>
          <string>magnetometer</string>
            <string>gps</string>
      <string>location-services</string>
            <string>armv7</string>
        </array>
    
    <key>NSLocationWhenInUseUsageDescription</key>
        <string>Desc</string>
        <key>NSLocationAlwaysUsageDescription</key>
        <string>Des1</string>
        <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
        <string>Des7</string>
       <key>NSLocationUsageDescription</key>
       <string>Desc123</string>

reference.d.ts

/// <reference path="./node_modules/@nativescript/types-ios/complete.d.ts"/>

When I call the startUpdatingHeading method from my component, the locationManagerDidUpdateHeading() method in the LocationMangerDelegate Class is not being triggered.

How can I make this work? I am stuck here and unable to move forward.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Pein
  • 423
  • 3
  • 11
  • 27

1 Answers1

0

I was able to get the heading using

   private locationManager : CLLocationManager;
   constructor() {
      this.locationManager = CLLocationManager.alloc().init();
      this.locationManager.desiredAccuracy = 3;
      this.locationManager.distanceFilter = 0.1;
      this.locationManager.headingFilter = 0.1;
    }
    
    getDirection(): number {
    return this.locationManager?.heading?.trueHeading === undefined ? -1 :          this.locationManager?.heading?.trueHeading;
}
Pein
  • 423
  • 3
  • 11
  • 27