简体   繁体   English

如何制作可在Android中找到/指向特定坐标的指南针

[英]How to make a compass that finds/points at specific coordinates in android

I'm trying to make a compass that points at a specific Longitude/Latitude, but I don't know how this is made, so I'll appreciate any help :) 我正在尝试制作一个指向特定经度/纬度的指南针,但我不知道它是如何制成的,因此,我将不胜感激:)

This is the compass. 这是指南针。 What do I've to add/change so that I can decide the Longitude/Latitude? 我必须添加/更改什么才能确定经度/纬度?

public class MainActivity extends Activity implements SensorEventListener {

private ImageView mPointer;
private SensorManager mSensorManager;
private Sensor mAccelerometer;
private Sensor mMagnetometer;
private float[] mLastAccelerometer = new float[3];
private float[] mLastMagnetometer = new float[3];
private boolean mLastAccelerometerSet = false;
private boolean mLastMagnetometerSet = false;
private float[] mR = new float[9];
private float[] mOrientation = new float[3];
private float mCurrentDegree = 0f;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
    mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    mMagnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
    mPointer = (ImageView) findViewById(R.id.pointer);
}

protected void onResume() {
    super.onResume();
    mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_GAME);
    mSensorManager.registerListener(this, mMagnetometer, SensorManager.SENSOR_DELAY_GAME);
}

protected void onPause() {
    super.onPause();
    mSensorManager.unregisterListener(this, mAccelerometer);
    mSensorManager.unregisterListener(this, mMagnetometer);
}

@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor == mAccelerometer) {
        System.arraycopy(event.values, 0, mLastAccelerometer, 0, event.values.length);
        mLastAccelerometerSet = true;
    } else if (event.sensor == mMagnetometer) {
        System.arraycopy(event.values, 0, mLastMagnetometer, 0, event.values.length);
        mLastMagnetometerSet = true;
    }
    if (mLastAccelerometerSet && mLastMagnetometerSet) {
        SensorManager.getRotationMatrix(mR, null, mLastAccelerometer, mLastMagnetometer);
        SensorManager.getOrientation(mR, mOrientation);
        float azimuthInRadians = mOrientation[0];
        float azimuthInDegress = (float)(Math.toDegrees(azimuthInRadians)+360)%360;
        RotateAnimation ra = new RotateAnimation(
                mCurrentDegree, 
                -azimuthInDegress,
                Animation.RELATIVE_TO_SELF, 0.5f, 
                Animation.RELATIVE_TO_SELF,
                0.5f);

        ra.setDuration(250);

        ra.setFillAfter(true);

        mPointer.startAnimation(ra);
        mCurrentDegree = -azimuthInDegress;
    }
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // TODO Auto-generated method stub

}

}

Maybe it's a little unclear. 也许还不清楚。 Let's say that I'm outside and I want to know the direction of a specific place. 假设我在外面,我想知道特定地点的方向。 This app should help me finding the direction 这个程序应该可以帮助我找到方向

You do not need compass for this. 您不需要为此使用指南针。 You need to get the current location and then use the method bearingTo in the Location class. 您需要获取当前位置,然后在Location类中使用bearingTo方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM