A pedido de un usuario hoy veremos como obtener la altitud, longitud y precisión del gps, para ello nosotros ya trabajamos como obtener la dirección del gps en android lo nuevo que veremos hoy aquí es la altitud y la precisión. Así que sin mas demora veamos de una vez y lo fácil que es obtener estos datos de nuestro gps.
Contenidos
Como obtener la altitud, precisión, latitud y longintud del gps

obtener altitud del gps en android
Tenemos que insertar algunos textview en nuestro activity_main
activity_main
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="0dp" android:layout_marginTop="20dp" android:text="GPS" android:textSize="40sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/txtLat" android:layout_width="100dp" android:layout_height="26dp" android:layout_marginStart="16dp" android:layout_marginTop="128dp" android:text="Latitud: " android:textSize="20sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/txtLong" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="20dp" android:layout_marginTop="20dp" android:text="Longitud: " android:textSize="20sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/txtLat" /> <TextView android:id="@+id/txtPre" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="20dp" android:layout_marginTop="20dp" android:text="Precision: " android:textSize="20sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/txtLong" /> <TextView android:id="@+id/txtAlt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="20dp" android:layout_marginTop="20dp" android:text="Altitud:" android:textSize="20sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/txtPre" /> </android.support.constraint.ConstraintLayout>
Ahora el código en Java
MainActivity
public class MainActivity extends AppCompatActivity { LocationManager locationManager; LocationListener locationListener; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); locationListener = new LocationListener() { @Override public void onLocationChanged(Location location) { updateLocationInfo(location); } @Override public void onStatusChanged(String s, int i, Bundle bundle) { } @Override public void onProviderEnabled(String s) { } @Override public void onProviderDisabled(String s) { } }; if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1); } else { locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); if (lastKnownLocation != null) { updateLocationInfo(lastKnownLocation); } } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { startListening(); } } public void startListening() { if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); } } public void updateLocationInfo(Location location) { TextView latitud = findViewById(R.id.txtLat); TextView longitud = findViewById(R.id.txtLong); TextView presicion = findViewById(R.id.txtPre); TextView altitud = findViewById(R.id.txtAlt); latitud.setText("Latitud: " + Double.toString(location.getLatitude())); longitud.setText("Longitud: " + Double.toString(location.getLongitude())); presicion.setText("Precision: " + Double.toString(location.getAccuracy())); altitud.setText("Altitud: " + Double.toString(location.getAltitude())); } }
Hola soy Alex Céspedes fundador de ANDROFAST, programo algunas cosas por diversión, me gusta aprender cosas nuevas y estoy pendiente de todo lo que tenga que ver con tecnología. Este blog lo cree para todas las personas que tengan dificultades en la programación, para ser sincero nunca fui bueno y reprobé algunos cursos de programación, pero mis ganas de aprender pudieron más. SI YO PUEDO TU PUEDES ANIMO!
wahuuu muchísimas gracias estimado! voy a probar un rato mas el código y te comento 🙂 gracias por tu tiempo y los grandes tutoriales que subes de android 🙂 saludos!!!
Amigo genial! acabo de probar el codigo y funciona perfecto, solo que en la “latitud” no me arroja ningun valor… en el manifest agregue solo el permiso o sera que hay que agregar otro permiso? saludos cordiales!
Gracias! subscrito y todo ok!!! saludos.