Home Ask Login Register

Developers Planet

Your answer is one click away!

Paddy S February 2016

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()));  

Answers


Luke Park February 2016

The Location class is simply an object used to store the result of a location update from the Location Provider.

Calling setLatitude and setLongitude won't just magically fill in the altitude at that position for you.


imerso February 2016

As far as I know, a Location will only have an altitude when triangulated by the GPS itself, using the satellites as points of reference. When you get the Location from the provider, it does that to calculate altitude, but when you force specific coordinates you don't get that.

To know the height of a specific coordinate (different from the current GPS coordinate), you should query that info from a map (ex: bing3d or openstreetmap).


CommonsWare February 2016

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.

Post Status

Asked in February 2016
Viewed 1,281 times
Voted 5
Answered 3 times

Search




Leave an answer


Quote of the day: live life