安卓定位中的getBearing()方法总是返回0

10

我一直在尝试为我的Android应用程序实现一个功能,即获取设备的速度和行进方向,无论设备指向何处。例如:如果我的Android设备指向北方,并且我向南方后退,它将返回我正在向南移动。

我一直在寻找方法,我想到可能可以使用Location类的getBearing()方法(但我不知道这是否能解决我的整个问题)。当我调用getBearing()时,由于某种原因它总是返回0.0。我不知道为什么。以下是我的代码:

LocationManager lm;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gcm);
    setUpUI(findViewById(R.id.LinearLayout1));
    isRegged = false;

    // GCM startup
    gcm = GoogleCloudMessaging.getInstance(this);
    context = getApplicationContext();

    gps = new GPSTracker(context);
    // gps.startListening(context);
    // gps.setGpsCall(this);

    /*
     * Variables to indicate location and device ID
     */
    TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

    if (gps.getIsGPSTrackingEnabled())
    {
        longitude = Double.valueOf(gps.getLongitude()).toString();
        latitude = Double.valueOf(gps.getLatitude()).toString();
    }

    deviceID = telephonyManager.getDeviceId();

    mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

    lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, (float) 0.0,
            this);
}

这是我获取方位的位置。

@Override
public void onLocationChanged(Location currentLocation)
{
    float speed = 0;
    float speed_mph = 0;

    if (previousLocation != null)
    {
        float distance = currentLocation.distanceTo(previousLocation);

        // time taken (in seconds)
        float timeTaken = ((currentLocation.getTime() - previousLocation
                .getTime()) / 1000);

        // calculate speed
        if (timeTaken > 0)
        {
            speed = getAverageSpeed(distance, timeTaken);
            speed_mph = (float) (getAverageSpeed(distance, timeTaken) / 1.6);
        }

        if (speed >= 0)
        {
            info_text.setVisibility(View.VISIBLE);
            info_text_mph.setVisibility(View.VISIBLE);

            DecimalFormat df = new DecimalFormat("#.#");
            info_text.setText("Speed: " + df.format(speed) + " " + "km/h");
            info_text_mph.setText("  Speed: " + df.format(speed_mph) + " "
                    + "mph");

            if (speed >= 10 && lm.getProvider(LocationManager.GPS_PROVIDER).supportsBearing())
            {
                float degree = currentLocation.getBearing();

                direction_text.setVisibility(View.VISIBLE);

                Log.i(TAG, String.valueOf(degree));

                if (degree == 0 && degree < 45 || degree >= 315
                        && degree == 360)
                {
                    direction_text.setText("You are: Northbound");
                }

                if (degree >= 45 && degree < 90)
                {
                    direction_text.setText("You are: NorthEastbound");
                }

                if (degree >= 90 && degree < 135)
                {
                    direction_text.setText("You are: Eastbound");
                }

                if (degree >= 135 && degree < 180)
                {
                    direction_text.setText("You are: SouthEastbound");
                }

                if (degree >= 180 && degree < 225)
                {
                    direction_text.setText("You are: SouthWestbound");
                }

                if (degree >= 225 && degree < 270)
                {
                    direction_text.setText("You are: Westbound");
                }

                if (degree >= 270 && degree < 315)
                {
                    direction_text.setText("You are: NorthWestbound");
                }

            }

        }
    }
    previousLocation = currentLocation;

}

非常感谢!

1个回答

6

getBearing()方法,如果使用LocationManager.NETWORK_PROVIDER获取数据,则会返回0,因为信号/精度太弱。尝试将GPS提供程序设置为GPS并确保在室外进行测试(由于卫星没有直接通信,因此GPS无法在室内或非常高的建筑物中使用)

为确保您选择的提供程序支持getBearing(),您可以使用LocationProvider中的方法supportsBearing(),如果您选择的提供程序支持getBearing()调用,则返回true。

最后,请确保您在AndroidManifest.xml文件中拥有ACCESS_COARSE_LOCATIONACCESS_FINE_LOCATION权限。

按照我的建议编写的代码应该是这样的:

LocationManager mlocManager =

(LocationManager)getSystemService(Context.LOCATION_SERVICE);

LocationListener mlocListener = new MyLocationListener();


mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);

资源: http://developer.android.com/reference/android/location/LocationManager.html http://developer.android.com/reference/android/location/LocationProvider.html http://www.firstdroid.com/2010/04/29/android-development-using-gps-to-get-current-location-2/

更新:答案是getBearing()中用于计算的两个点太接近,因此导致结果不准确。为了纠正这一问题,需要手动获取两个GPS点并使用bearingTo()函数以获得更准确的结果。


是的,我试过了。我在开车时尝试测试该功能。虽然速度相当准确(误差为5-10公里),但getBearing()始终返回0,这总是返回“您正在:向北行驶”,无论我朝哪个方向前进。 - user3171597
使用bearingTo()方法后,方位角不再总是返回0!我必须进一步测试当我在驾驶时是否准确。然而,它有效。非常感谢!另外,我的getBearing()为什么每秒钟总是返回随机浮点数?这可能是由于LocationManager的最小距离和最小更新时间造成的吗? - user3171597
太棒了!getBearing()返回的是度数,但如果两个点之间距离太近,则数字将不准确(或“随机”)。 - Matt Newbill
大家好,有没有办法在室内环境中获取准确的方向信息呢?例如,iOS使用CoreLocation框架提供trueHeading。 在Android中是否有类似的功能?我的详细问题在这里http://stackoverflow.com/questions/32693424/corelocation-heading-android。 - chintan s
1
@The_Martian 保留一个变量来存储旧位置或1-2秒前的位置,并手动计算方向。如果每次都这样做,它将不足以提供正确的方向。 - Matt Newbill
显示剩余8条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接