当我找不到北向时如何用本机脚本制作指南针?

时间:2019-01-21 13:16:46

标签: nativescript

我正在处理nativescript,但是其中严重缺少库。我找不到北向。 我尝试过插件

   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;
    }

也有direction属性可用,但始终为-1。 我正在使用Typescript和javascript作为模板语言。

1 个答案:

答案 0 :(得分:1)

对于iOS和Android:

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;
    }
  }
}