Kenneth Breugelmans
Kenneth Breugelmans

Reputation: 561

Android how to get gps Bearing

I'm building an android application that requires my location and bearing.

When I use location.getBearing() the result is 0.0

The google maps application seems to have my bearing just fine when I walk around the house. My question is how can I get my bearing so it works as flawlessly as in google maps.

I've tried calculating it by using a previous known location with a distance of at least 2 meters, and getting the bearing to that location but it's all over the place.

Thanks

Upvotes: 2

Views: 7877

Answers (2)

corn3lius
corn3lius

Reputation: 5005

from the android developer page

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.

I would suggest you use the hasBearing() call to determine if you can use the bearing.

edit

I would change your requestLocationUpdates parameters reasonable values. Read here : https://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates

minTime and minDistance should be used to limit using too much of the devices battery. I would suggest ( 2000, 2.0 ) respectfully. (2 seconds or 2 meters)

Upvotes: 1

Kenneth Breugelmans
Kenneth Breugelmans

Reputation: 561

I found the solution to the problem. locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);

The first 0 represents the minimum time before it updates again. By setting it at 0, for some reason the gps isn't getting the bearing. The problem is resolved by simply changing it to a higher number. Like this; locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,2,0,locationListener);

Upvotes: 0

Related Questions