me gustaría poder determinar mi actual localización en un proyecto android, para así poder obtener la latitud y longitud de mi posición para usarlos en un método en especifico, he revisado las preguntas resueltas, pero no entiendo el código que han dado como respuesta, si pudieran darme una mano, se los agradecería
2 respuestas
Tal como en unas de las respuestas que te dieron en otras preguntas, se utiliza LocationManager
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
Esto te traera la posicion actual, de no haber una posicion actual devolvera null obviamente pero hay un metodo que hace este trabajo de forma asincrona
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
longitude = location.getLongitude();
latitude = location.getLatitude();
}
}
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);
Como obtener latitud y longitud al iniciar la aplicación:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(bestProvider);
if (location != null) {
onLocationChanged(location);
}
locationManager.requestLocationUpdates(bestProvider, 20000, 0, this);
-
-
-
-
1
-
1
Viste la documentación de apache cordova? ahí sale como hacerlo, yo lo logre pero en ionic. Te dejo el link, saludos.
https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-geolocation/