0

I am working on nativescript , but There are serious lack of libraries in it. I am not able to find north direction. i have tried plugin

   import * as geolocation from "nativescript-geolocation";
    import { Accuracy } from "tns-core-modules/ui/enums"; 
         test:function() {

          var a = geolocation.getCurrentLocation({ desiredAccuracy: Accuracy.high, maximumAge: 5000, timeout: 20000 })
                    ;
                    a.then( return_a => {
                    console.log("this------------------------------->",return_a);        
                    //distance(return_a,);
                    var degree = angleFromCoordinate(return_a.latitude,return_a.longitude , 21.4225,39.8262);
                     console.log(degree);
                    this.gaugeValue = degree;//return_a.verticalAccuracy

                    });   
                    }
          });
        function angleFromCoordinate( lat1,  long1,  lat2, long2) {

        var dLon = (long2 - long1);

        var y = Math.sin(dLon) * Math.cos(lat2);
        var x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1)
                * Math.cos(lat2) * Math.cos(dLon);

        var brng = Math.atan2(y, x);

        brng = brng * 180 / Math.PI;
        brng = (brng + 360) % 360;
        brng = 360 - brng; // count degrees counter-clockwise - remove to make clockwise

        return brng;
    }

There is direction property also available.but it is always -1 . i am using Typescript , javascript as template language.

James Z
  • 12,209
  • 10
  • 24
  • 44
Amit Sharma
  • 1,968
  • 5
  • 24
  • 35
  • The purpose of nativescript-geolocation plugin was to deal with location services alone, I don't think it implements required apis to detect heading. Also there is no ready made plugin available to accomplish this functionality, you will have to write one or directly access the native apis. – Manoj Jan 21 '19 at 13:39
  • First you need to verify that the user will has a device with actual hardware sensors (not NativeScript related) and only after that, you can access and use the native Android API to detect the geographic direction (more here https://stackoverflow.com/questions/8315913/how-to-get-direction-in-android-such-as-north-west) – Nick Iliev Jan 21 '19 at 14:24

1 Answers1

2

Here I found the solution. You need to do it manually.

import * as app from "tns-core-modules/application"; 
import { isAndroid, isIOS } from "tns-core-modules/platform";

declare const android: any;
declare const CLLocationManager: any;

export class MyClass {
  private sensorUpdate: any;
  private sensorManager: any;

  startHeadingUpdates() {
    if (this.sensorManager || this.sensorUpdate) {
      return;
    }

    if (isIOS) {
      this.sensorManager = CLLocationManager.alloc().init();

      if (this.sensorManager.headingAvailable) {
        this.sensorManager.startUpdatingHeading();

        this.sensorUpdate = setInterval(() => {
          console.log(this.sensorManager.heading.trueHeading);
        }, 100);
      } else {
        console.log("Heading not available.");
      }

      return;
    }

    if (isAndroid) {
      this.sensorManager = app.android.foregroundActivity.getSystemService(
        android.content.Context.SENSOR_SERVICE
      );

      this.sensorUpdate = new android.hardware.SensorEventListener({
        onAccuracyChanged: (sensor: any, accuracy: any) => {
          // console.log(accuracy)
        },
        onSensorChanged: (event: any) => {
         console.log(event.values[0]);
        }
      });

      const orientationSensor = this.sensorManager.getDefaultSensor(
        android.hardware.Sensor.TYPE_ORIENTATION
      );
      this.sensorManager.registerListener(
        this.sensorUpdate,
        orientationSensor,
        android.hardware.SensorManager.SENSOR_DELAY_UI
      );
    }
  }

  stopUpdatingHeading() {
    if (!this.sensorManager || !this.sensorUpdate) {
      return;
    }

    if (isIOS) {
      this.sensorManager.stopUpdatingHeading();
      clearInterval(this.sensorUpdate);
      this.sensorManager = null;
      return;
    }

    if (isAndroid) {
      this.sensorManager.unregisterListener(this.sensorUpdate);
      this.sensorManager = null;
    }
  }
}