Android new location API Altitude is always zero

2019-07-22 02:00发布

问题:

I am using Google new location Api in my app like this

private static final LocationRequest REQUEST = LocationRequest.create().setInterval(2000).setFastestInterval(1000)
        .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

private LocationListener mLocationListener = new LocationListener() {

    @Override
    public void onLocationChanged(final Location location) {
        if(location.hasAltitude()){
            Log.e(TAG, "Altitude " + location.getAltitude());
        }else{
            Log.e(TAG, "Location does not have altitude");
        }
    }
};

but location.hasAltitude() always false, I have tested it on real device and in open area, but it does not have altitude anywhere, but if I am using the old location Listener, its working fine,

回答1:

As of 3.1.36 version of the API fused location provider always returns false there (at least for me when having GPS turned on), so you may want to stick with LocationManager and GPS provider.

Hopefully they will improve the fused algorithm in the future release.