如何在android上制作一个精确的指南针

发布于 2024-12-11 18:07:32 字数 1288 浏览 0 评论 0原文

我的Android应用程序显示了世界上特定地点的方向,因此需要获得罗盘度数。
这是我一直用来计算度数的代码:

public void getDirection() {        
    mySensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
    List<Sensor> mySensors = mySensorManager.getSensorList(Sensor.TYPE_ORIENTATION);
    if(mySensors.size() > 0){
        mySensorManager.registerListener(mySensorEventListener, mySensors.get(0), SensorManager.SENSOR_DELAY_UI);           
    }
    else{
        TextView alert = (TextView)findViewById(R.id.instruct);
        alert.setText(getString(R.string.direction_not_found));
        myCompassView.setVisibility(myCompassView.INVISIBLE);

    }
}
private SensorEventListener mySensorEventListener = new SensorEventListener(){

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

    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        // TODO Auto-generated method stub
        compassBearing = (float)event.values[0];
        float bearing;
        bearing = compassBearing - templeBearing;
        if (bearing < 0)
            bearing = 360 + bearing;
        myCompassView.updateDirection(bearing);
        }
};

这种方法通常有效,但有时它只是得到错误的北,我需要做什么才能获得更准确的位置?

my android application shows the direction of a particular place in the world and therefore in needs to get the compass degree.

This is the code I've been using to calculate the degrees:

public void getDirection() {        
    mySensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
    List<Sensor> mySensors = mySensorManager.getSensorList(Sensor.TYPE_ORIENTATION);
    if(mySensors.size() > 0){
        mySensorManager.registerListener(mySensorEventListener, mySensors.get(0), SensorManager.SENSOR_DELAY_UI);           
    }
    else{
        TextView alert = (TextView)findViewById(R.id.instruct);
        alert.setText(getString(R.string.direction_not_found));
        myCompassView.setVisibility(myCompassView.INVISIBLE);

    }
}
private SensorEventListener mySensorEventListener = new SensorEventListener(){

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

    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        // TODO Auto-generated method stub
        compassBearing = (float)event.values[0];
        float bearing;
        bearing = compassBearing - templeBearing;
        if (bearing < 0)
            bearing = 360 + bearing;
        myCompassView.updateDirection(bearing);
        }
};

This method usually works but sometimes it just gets the wrong north, what do I have to do to get a more accurate location?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最笨的告白 2024-12-18 18:07:32

我有一些建议给您:

1)您的设备可能未校准。为此,请以 8 的形式移动它(

现在,不要忘记传感器为您提供的是磁北方向,而不是真北方向!这种差异被称为磁场偏角,由于地球磁场的变化,其值随地点和时间而变化。 此应用可以为您计算一些值相对准确度。我不会太担心这一点,因为赤纬通常很小,但您可能正在寻找良好的精度(目前我住的地方赤纬为 3°)。

2)远离金属物体或产生强磁场的东西。例如,如果您的手机靠近计算机或任何物理键盘,请勿进行测试!这对于测试指南针地理定位来说是纯粹的毒药。某些应用可以测量磁场强度 (如果设备支持)。当你接近金属的东西时,你会体验到更高的价值和方向上的强烈变化。为了好玩,还有一些“金属探测器” :当您靠近“金属物体”或对设备产生磁性干扰的物体时,此应用程序会识别磁场的变化并振动。

3) 当您将设备倾斜至横向模式时,请记住更新方位。 (文章是必读的!< /strong>)这是因为方位角值基于垂直于手机平面的轴的旋转。当您将设备旋转至横向时,该值会更改 +/-90°! 通过禁用应用程序横向模式无法解决此问题!您必须通过分析围绕其他两个轴(俯仰和滚动)的旋转以编程方式确定它。这并不是微不足道的,但网络上有一些例子。

编辑:如果您对某些代码感兴趣,请查看Mixare,它是一个遵循 GPL3 的 Android 开源增强现实框架。看看他们关于方向、罗盘地理位置和方位的代码。

PS:我与上述应用程序的创建者没有任何联系。

I have a couple suggestions for you:

1) Your device may not be calibrated. In order to do it, move it around in a form of 8 (see this). If you don't if your device is calibrated or not make some tests by pointing the device at some known cardinal point direction and compare the values. Typically, if a device is not calibrated, you will see great variations in the azimuth value for small rotations. That is what I would be worried about.

Now, don't forget that the sensor gives you the bearing to Magnetic North, and not True North! This difference is known as declination of the magnetic field and its value changes from place to place and from time to time due to changes in Earth's magnetic field. This app can compute some of the values for you with relative accuracy. I wouldn't be too much worried about this as the declination is typically small, but you might be looking for good precision (where I live the declination is 3º, currently).

2) Stay away from metal objects or stuff that generate a strong magnetic field. For example, don't do tests if you have your phone near the computer or any physical keyboards! This is pure poison for testing compass-geolocation. Some apps can measure the intensity of the magnetic field (if the device supports it). When you get closer to metal stuff you will experience higher values and strong changes in directions. For fun, there are also some "metal detectors": this app recognises changes in the magnetic field and vibrates when you are close "metal object" or stuff that magnetically interfere with the device.

3) Remember to update the bearing when you tilt your device to landscape mode. (article is a must read!) This is because azimuth value is based on the rotation of the perpendicular axis to the plane of the phone. When you rotate the device to landscape, this value is changed by +/-90º! This is not resolved by disabling the application landscape mode! You will have to determine it programmatically by analysing rotations around the other two axis (pitch and roll). This is not trivial, but there are some examples somewhere in the net.

edit: If you are interested in some code, check out Mixare, it is an open source augmented reality framework under the GPL3 for Android. Take a look at their code regarding orientation, compass geolocation and bearing.

PS: I don't have any sort of connection with the creators of the mentioned applications.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文