0

I've implemented an app that uses GPS, with google API. As far as I read the documentation, the Location.getAltitude returns a double, but all the values I got are round integer numbers.

I wanted the value with at least 1 decimal place.

Am I doing something wrong, or is it expected to behave like that?

Here's relevant parts of my code:

protected void createLocationRequest() {
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(UPDATE_INTERVAL);
    mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setSmallestDisplacement(DISPLACEMENT);
}

@Override
public void onLocationChanged(Location locationRead) {

//a lot of stuff, check signal, filter wrong reading, etc...

currentLocation = locationRead;

//compare last location with current location, some calculation, etc...

//already used String.format("%.1f",currentLocation.getLongitude()).replace(",", "."),
DecimalFormat df = new DecimalFormat("#.0");

//Send information to another class
GPS.this.interfaceGPS_Activity.locationChanged(
                (currentLocation.distanceTo(lastLocation) / 1000),
                (timeSplit),
                formatter.format(dateTimeOfGPS),
                String.format("%.7f", currentLocation.getLatitude()).replace(",", "."),
                String.format("%.7f", currentLocation.getLongitude()).replace(",", "."),
                df.format(currentLocation.getAltitude()).replace(",","."),
                currentPaceForLabel
        );

The altitude sent is always xyz.0, like 920.0 921.0 1011.0, it's never 920.6 or 921.2 always .0

  • Beyond Gabe's correct answer, even if it *were* possible for GPS to give you sub-meter altitude accuracy, that does not mean that *every Android device* will return you sub-meter accuracy. Android devices, and their GPS radios, vary widely in quality and capability. – CommonsWare Dec 04 '15 at 22:28

1 Answers1

1

You aren't doing anything wrong, it just isn't that accurate. GPS doesn't even give you 1m of accuracy, much less tenth of a meter.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • Thank you Gabe. I did not know that. I was mislead by the "double" returned by the getAltitude. Anyway, my issue is when I calculate the total elevation gain, I guess I will do some sort of "smoothing" of the values, and then, calculate the elevation gain. Sometime I get like -1, +1 variations on a long plan route, and when I sume up, it will give 100m of gain, which is not true. I will do some work around, and also, implement an alternative with the barometer, for whoever has one... – Willyan Vincenzi Dec 04 '15 at 23:11
  • I answered something like that problem for someone trying to do cumulative distance tracking yesterday- http://stackoverflow.com/questions/34079118/android-how-can-i-calculate-the-cumulative-distance-with-gps/34079198#34079198 Basically getting what you want to do to be accurate is going to require some work to get a good answer. – Gabe Sechan Dec 05 '15 at 05:52
  • You can get considerably better accumulated elevation gain / loss numbers on phones with a pressure sensor by using that but you still have to apply some filtering and work to make on the flat return zero. With some phone gps chipsets you may get quite big altitude jumps say 10m up and down even when on a very flat terrain it's very hard to deal with. An online lookup is the best solution often in this case in flat terrain but not good in hilly terrain. – Ifor Dec 06 '15 at 21:42