簡體   English   中英

Android Location getBearing()始終返回0

[英]Android Location getBearing() always returns 0

我一直在嘗試為我的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;

}

非常感謝!

如果您使用LocationManager.NETWORK_PROVIDER獲取數據,則getBearing()將返回0,因為信號/准確性太弱。 嘗試將GPS提供程序設置為GPS,並確保在室外對其進行測試(由於衛星之間沒有直接通訊,GPS無法在室內或在很高的建築物中間使用)

為了確保你選擇支持getBearing提供商(),您可以使用從方法LocationProvider稱為supportsBearing ()如果提供您選擇支持的返回true getBearing()調用。

最后,確保您在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-get-current-location-2 /

更新:答案是,用於在getBearing()中計算的兩點太近了,因此得出的結果不准確。 要更正此問題,請手動獲取兩個GPS點,然后使用bearingTo()查看更准確的結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM