Crag
Crag

Reputation: 1841

What does Android Location hasBearing check?

I'd like to check if a bearing is available on a location, and, if so, use it. This is basically what I'm doing:

if (loc.hasBearing()) {
    // code that uses bearing
} else {
    // log no bearing
}

The code in the if block gets executed for lots of locations with a bearing of 0 (Math.floor'd), and 0 wasn't the actual bearing.

I do try to make sure I get bearings (or at least that I use a provider that supports bearing):

criteria.setBearingRequired(true);
criteria.setBearingAccuracy(Criteria.ACCURACY_MEDIUM); 

I am putting the location into an intent's extras and taking it out again... maybe that has something to do with it? I'm just using putExtra and getParcelable - I wouldn't think that would corrupt the locations.

I'm thinking of just ignoring bearings of 0.0, which shouldn't have too many false positives, but I was hoping there was a clean use of the API for me instead.

Upvotes: 2

Views: 821

Answers (1)

LordRaydenMK
LordRaydenMK

Reputation: 13321

From the documentation:

Get the bearing, in degrees.

Bearing is the horizontal direction of travel of this device, and is not related to the device orientation. It is guaranteed to be in the range (0.0, 360.0] if the device has a bearing.

If this location does not have a bearing then 0.0 is returned.

Since getBearing() returns 0.0 if the location has no bearing you can safly ignore it.

Upvotes: 2

Related Questions