1

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()));  
Paddy S
  • 45
  • 6
  • What should it be returning? – BSMP Feb 06 '16 at 20:34
  • 1
    You understand that `locationProvider.supportsAltitude();` returns a `boolean` value right? Have you actually checked if this value is true? Because the way you are using it currently does literally nothing. – Luke Joshua Park Feb 06 '16 at 20:35
  • It should return the altitude if available, in meters above the WGS 84 reference ellipsoid. I have tried several coordinates but result is the same. – Paddy S Feb 06 '16 at 20:43
  • Yes Luke it returns True – Paddy S Feb 06 '16 at 20:44
  • Well I meant, "What specific altitude should be returned for the coordinates in your code?" but I guess that's moot. – BSMP Feb 07 '16 at 02:36

3 Answers3

1

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.

Luke Joshua Park
  • 9,527
  • 5
  • 27
  • 44
  • So calling the getAltitude () on a Location object is incorrect? In an example I was following that retrieves the users location I applied the getAltitude() but the results were the same. – Paddy S Feb 06 '16 at 20:53
  • The Location object that they were using was likely created by a Location Provider. – Luke Joshua Park Feb 06 '16 at 20:57
  • Just checked, you are correct they do use a Location provider. Would you have any suggestions on how I might get the altitude another way? – Paddy S Feb 06 '16 at 21:09
  • If my answer helped, please mark it as the solution. I would recommend looking at the Google Maps Elevation API: https://developers.google.com/maps/documentation/elevation/intro?hl=en – Luke Joshua Park Feb 06 '16 at 21:10
  • Thanks, was hoping I wouldn't have to go down that road but looks like I have no choice. Thanks again for the help. – Paddy S Feb 06 '16 at 21:16
0

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).

imerso
  • 511
  • 4
  • 12
0

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • That is my problem. I am manual assembling a Location object and then calling the getAltitude (). I will look into the LocationManager route. Thanks – Paddy S Feb 06 '16 at 21:31