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);
}
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With