Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get altitude in Android

Tags:

android

I use the Location class to get altitude, but it always returns 0.

Please tell me how to get altitude?

Location.getAltitude();
like image 576
Smith Avatar asked Oct 26 '25 10:10

Smith


2 Answers

Initially I used location.getAltitude() and it always retuned 0.0 . Then I came across https://groups.google.com/forum/#!topic/android-beginners/KNeLh905ip0 it helped me solve my problem. It works on actual real device.

private LocationProvider _locationProvider;
private LocationManager _locationManager;
   _locationManager = (LocationManager) mContext
            .getSystemService(LOCATION_SERVICE);

    _locationProvider = _locationManager
            .getProvider(LocationManager.GPS_PROVIDER);

     location = _locationManager.getLastKnownLocation 
                (LocationManager.GPS_PROVIDER); 

     _locationProvider.supportsAltitude();                          
            hasaltitude = location.hasAltitude();
            double altitude = location.getAltitude();
            System.out.println("HasAltitude" + hasaltitude+"-"+altitude);

The value of "altitude" is will be in meters.

like image 177
Anam Ansari Avatar answered Oct 29 '25 00:10

Anam Ansari


Altitude is typically only available with GPS provider. You can check with available location provider if the support altitude:

locationProvider.hasAltitude();
like image 27
Peter Knego Avatar answered Oct 28 '25 22:10

Peter Knego