Search code examples
androidgps

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


Solution

  • 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)