Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Altitude always 0. I cannot obtain the altitude above sea level

I get the altitude always 0. I want to obtain the altitude above sea level in Android using GPS.

My code is:

public void onLocationChanged(Location location) {
    double lat = (double) (location.getLatitude());
    double lng = (double) (location.getLongitude());
    double alt = (double) (location.getAltitude());
    latitudine = (int) (lat * 1e6);
    longitudine = (int) (lng * 1e6);
    altitudine = (int) (alt * 1e6);
}
like image 536
AndreaF Avatar asked Oct 22 '25 03:10

AndreaF


1 Answers

Did you check if this is device specific? Are you working in an emulator?

I would at first check, if your device supports the altitude determination with:

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

If you want to know if a location has a valid altitude set, use the following:

Location location;

/* Get current location from GPS */
location = Location(LocationManager.GPS_PROVIDER);

/* Check if the requested Location has its altitude set */
location.hasAltitude();
like image 162
Nippey Avatar answered Oct 24 '25 17:10

Nippey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!