Question

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);
}
Was it helpful?

Solution

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

OTHER TIPS

You should check whether the location contains altitude information: before calling location.getAltitude(), just call location.hasAltitude() to know if the returned location contains this type of information.

Depending on the location provider you use (NETWORK or GPS), it may not support altitude determination. If you have an application that works with altitude, try to find which location provider it is using.

If you want to check if a particular location provider supports altitude, first call LocationManager.getProvider(SensorManager.GPS_PROVIDER) to get the LocationProvider and then call the method locationProvider.supportsAltitude().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top