Question

I am using the Google Play Location Services to help users navigate along a path but I am not getting compass bearing information from the Location object I receive in onLocationChanged(Location location). I always get a false result from hasBearing() and getBearing() returns 0.0 in my LocationListener implementation.

Below is the code I am using with the noise code stripped out. It shows the parameters used to create a LocationRequest. The code quite happily jumps into the LocationListener implementation so it appears I have a working architecture for that part of the system.

public class IncrementalNavigator {
    private LocationClient locationClient;
    private LocationRequest locationRequest;
    private float headingBearing = 0.0f;

    private LocationListener locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            Log.d(TAG, "The location has changed to " + location.toString());

            if (location.hasBearing()) {
                // I never see this updated.
                headingBearing = location.getBearing();
            }

            // ...
        }
    };


    public IncrementalNavigator(LocationClient locationClient) {
        this.locationRequest = LocationRequest.create();
        this.locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        this.locationRequest.setInterval(5000);
        this.locationRequest.setFastestInterval(1000);
        this.locationClient = locationClient;
        this.headingBearing = locationClient
                .getLastLocation()
                .getBearing();

        // ...
    }

    public void setWalkingDirections(WalkingDirections walkingDirections) {
        // ...

        if (walkingDirections != null) {
            // ...
            this.locationClient.requestLocationUpdates(locationRequest, locationListener);
        } else {
            this.locationClient.removeLocationUpdates(locationListener);
        }
    }
    // ...
}

My AndroidManifest.xml declares the following permissions:

<uses-permission android:name="android.permission.ACCESS_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>

I am running the code as part of a Service on a Galaxy Nexus with Android 4.3. WiFi is turned on and GPS is turned on. My question is: why am I not receiving bearing updates?

Était-ce utile?

La solution

This code is functional it is just not the correct way to get compass bearing data. After taking the app outdoors and getting a GPS fix I was able to get new readings. I have since re-read the documentation and have realised the bearing on location is not a compass bearing (i.e. north is not 0.0) but the horizontal direction of travel.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top