Android指南针问题 [英] Android compass issue

查看:112
本文介绍了Android指南针问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我试图制作一个可通过指南针跟踪手机指向哪个方向的应用程序,一旦在屏幕上按下一个按钮,它就会以度为单位显示其指向的位置数.到目前为止,我了解指南针是如何创建的,但是找不到相对于North的指向的值.这是我到目前为止所拥有的.

public class compass extends Activity implements OnClickListener, SensorEventListener{

    private final SensorManager DirPoint;
    float var;
    TextView theNumber;

    Button DirectionIn;

    public compass(){ 
        DirPoint = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    }

    public void onAccuracyChanged(Sensor sensor, int accuracy) {}
    public void onSensorChanged(SensorEvent event) {}

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        theNumber = (textView) findViewById(R.id.output);
        DirectionIn =(Button) findViewById(R.Id.Buton);

        DirectionIn.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                //gets direction of phone compass
                // ((TextView)findViewById(R.id.output)).setText(var);
            }
        }
    }
}

任何帮助都将受到欢迎,或者如果我朝正确的方向前进甚至会很好.

解决方案

您必须实现指南针".您可以这样操作:

让您的活动实现SensorEventListener并添加必要的字段:

public class CompassActivity extends Activity implements SensorEventListener {

    private SensorManager sensorManager;
    private Sensor accelerometer;
    private Sensor magnetometer;
    private float[] lastAccelerometer = new float[3];
    private float[] lastMagnetometer = new float[3];
    private boolean lastAccelerometerSet = false;
    private boolean lastMagnetometerSet = false;
    private float[] rotationMatrix = new float[9];
    private float[] orientation = new float[3];
    private float currentDegree = 0f;

在活动的onCreate方法中,获取并启动两个传感器,即加速度计和磁力计:

        // onCreate method stub ...
        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        magnetometer = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
        sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI);
        sensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_UI);
        // more onCreate method stub ....

现在,在SensorEventListener的方法中,您可以计算电话的方向并计算当前位置和其他位置之间的方位:

    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor == this.accelerometer) {
            System.arraycopy(event.values, 0, this.lastAccelerometer, 0, event.values.length);
            this.lastAccelerometerSet = true;
        } else if (event.sensor == this.magnetometer) {
            System.arraycopy(event.values, 0, this.lastMagnetometer, 0, event.values.length);
            this.lastMagnetometerSet = true;
        }

        if (this.lastAccelerometerSet && this.lastAccelerometerSet) {
            SensorManager.getRotationMatrix(this.rotationMatrix,null, this.lastAccelerometer, this.lastMagnetometer);
            SensorManager.getOrientation(this.rotationMatrix, this.orientation);


            float azimuthInRadiands = this.orientation[0];

            // this is now the heading of the phone. If you want
            // to rotate a view to north don´t forget that you have
            // to rotate by the negative value.
            float azimuthInDegrees = (float) Math.toDegrees(azimuthInRadiands);

        }
    }

但不要忘记,指南针后面还有更多东西.您必须向用户展示磁场传感器是否未校准.您必须计算磁北和地理北之间的差...

我创建了一个小型指南针助手类. HowTo在自述文件中.它将为您提供在屏幕上显示指南针所需的所有信息:

GitHub上的指南针助手

它为您提供设备的当前标题.请随时问我是否有问题.

SO im trying to make a App that tracks which direction the phone is pointed VIA the compass and once a button is hit on the screen it displays the number of where it is pointed in degrees. So far i understand how the compass is created but can not find which values are the pointed direction in relation to North. Here is what i have so far.

public class compass extends Activity implements OnClickListener, SensorEventListener{

    private final SensorManager DirPoint;
    float var;
    TextView theNumber;

    Button DirectionIn;

    public compass(){ 
        DirPoint = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    }

    public void onAccuracyChanged(Sensor sensor, int accuracy) {}
    public void onSensorChanged(SensorEvent event) {}

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        theNumber = (textView) findViewById(R.id.output);
        DirectionIn =(Button) findViewById(R.Id.Buton);

        DirectionIn.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                //gets direction of phone compass
                // ((TextView)findViewById(R.id.output)).setText(var);
            }
        }
    }
}

Any help would be welcomed or if im headed in the right direction even would be nice.

解决方案

You have to implement a "compass". You can do this like this:

Let your activity implement the SensorEventListener and add the necessary fields:

public class CompassActivity extends Activity implements SensorEventListener {

    private SensorManager sensorManager;
    private Sensor accelerometer;
    private Sensor magnetometer;
    private float[] lastAccelerometer = new float[3];
    private float[] lastMagnetometer = new float[3];
    private boolean lastAccelerometerSet = false;
    private boolean lastMagnetometerSet = false;
    private float[] rotationMatrix = new float[9];
    private float[] orientation = new float[3];
    private float currentDegree = 0f;

In the onCreate method of the activity get and start the two sensors, the accelerometer and the magnetometer:

        // onCreate method stub ...
        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        magnetometer = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
        sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI);
        sensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_UI);
        // more onCreate method stub ....

In the method of the SensorEventListener you can now calculate the heading of the phone and calculate the bearing between the current location and a other location:

    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor == this.accelerometer) {
            System.arraycopy(event.values, 0, this.lastAccelerometer, 0, event.values.length);
            this.lastAccelerometerSet = true;
        } else if (event.sensor == this.magnetometer) {
            System.arraycopy(event.values, 0, this.lastMagnetometer, 0, event.values.length);
            this.lastMagnetometerSet = true;
        }

        if (this.lastAccelerometerSet && this.lastAccelerometerSet) {
            SensorManager.getRotationMatrix(this.rotationMatrix,null, this.lastAccelerometer, this.lastMagnetometer);
            SensorManager.getOrientation(this.rotationMatrix, this.orientation);


            float azimuthInRadiands = this.orientation[0];

            // this is now the heading of the phone. If you want
            // to rotate a view to north don´t forget that you have
            // to rotate by the negative value.
            float azimuthInDegrees = (float) Math.toDegrees(azimuthInRadiands);

        }
    }

But don´t forget that there is much more behind a compass. You have to show the user if the magnetic field sensor is uncalibrated. You have to calculate the difference between the magnetic and the geographic north...

I have created a small compass helper class. The HowTo is in the readme. It will provide you all the information you need to present a compass on the screen:

Compass Assistant on GitHub

It provides you the current heading of the device. Please don´t hesitate to ask me if you have problems.

这篇关于Android指南针问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆