Search code examples
androidmobileandroid-elevationandroid-gps

Altitude always returns 0.00


The return value from the getAltitude() method always returns 0.00. Many answers suggest it could be down to the provider not supporting altitude details. I have checked this and it does support altitude.

I want to obtain the altitude from GPS coordinates. Any help is appreciated.

My code can been seen below.

LocationManager locationManager; LocationProvider locationProvider;

/* Get LocationManager and LocationProvider for GPS */
 locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
 locationProvider = locationManager.getProvider(LocationManager.GPS_PROVIDER);

/* Check if GPS LocationProvider supports altitude */
locationProvider.supportsAltitude();

//Location object 
Location l = new Location("A");
l.setLatitude(40.796852);
l.setLongitude(-74.061340);
l.getAltitude();

//Returns 0.00
Log.i("Tag", String.valueOf(l.getAltitude()));

//returns True
Log.i("Tag", String.valueOf(locationProvider.supportsAltitude()));  

Solution

  • You are manually assembling a Location object. If you wish for that Location object to return a non-zero value for getAltitude(), you need to call setAltitude().

    If you wish to get device's current location, possibly including its altitude, you can use LocationManager, as is covered in the documentation.

    If you are trying to get the elevation above sea level of the surface of the Earth at a particular latitude and longitude, Android does not have anything for that. You will need to find some sort of library or Web service that has those details.