0

He estado programando una aplicación que requiere recuperar la latitud y la longitud, actualmente utilizo el siguiente Código.

¿Existe un método para preguntar la latitud y longitud sin necesidad de escucharlo constantemente?

He leído mucho sobre getlastknownlocation pero no tengo un ejemplo claro.

    private LocationListener listener;
private LocationManager locationManager;






@Override
public void onCreate() {





    listener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            String latitude = String.valueOf(location.getLatitude());
            String longitud = String.valueOf(location.getLongitude());
            Log.d("latitude", latitude);
            Log.d("longitud", longitud);

        }

        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {

        }

        @Override
        public void onProviderEnabled(String s) {
            Log.d("GPS", "ENABLE");
        }

        @Override
        public void onProviderDisabled(String s) {
            Log.d("GPS", "DISABLE");
        }

    };


    locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);



    //noinspection MissingPermission
    //locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0, listener);
    if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
             locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 12000, 0, listener);}
    if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 12000, 0, listener);
    }
    }


}
1

2 respuestas 2

1

Luego de leer, encuentro un ejemplo que me ayuda bastante para mi proyecto, el señor riteshreddyr

facilita un archivo que contiene las principales funciones del Fused-Location, dicho ejemplo optimiza los tiempos de obtención de latitud y longitud y es beneficioso para la batería.

Perfecto para lo que necesitaba. obtener la latitud y la longitud cada cierto tiempo sin usar un listener.

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;

import java.util.Calendar;

/**
    Interface for using Google's FusedLocationAPI.
*/
public class FusedLocation implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener {
    /**
     * The desired interval for location updates. Inexact. Updates may be more or less frequent.
     */
    public static final long UPDATE_INTERVAL_IN_MILLISECONDS = 1000;
    /**
     * The fastest rate for active location updates. Exact. Updates will never be more frequent
     * than this value.
     */
    public static final long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = UPDATE_INTERVAL_IN_MILLISECONDS / 2;
    private final static String TAG = "FUSED LOCATION";

    public Callback mCallback = null;
    /**
     * Stores parameters for requests to the FusedLocationProviderApi.
     */
    protected LocationRequest mLocationRequest;
    /**
     * Provides the entry point to Google Play services.
     */
    private GoogleApiClient mGoogleApiClient;
    private Context mContext;
    private Location mCurrentLocation = null;
    private int PRIORITY = LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY;
    private boolean inProgress = false;
    private int numTries = 0;
    private long diffTime = 5000;
    private float minAccuracy = 35;
    private int maxTries = 1;
    private boolean lastKnownLocation = false;

    public FusedLocation(Context c, Callback callback) {
        this.mContext = c;
        this.mCallback = callback;
    }
    
    /**
      Sets up FusedLocation Updates and chooses the best reading among `maxTries` samples.
      @param maxTries Maximum number of times to sample GPS readings
    */
    public void getCurrentLocation(int maxTries) {
        this.maxTries = maxTries;
        chooseNetworkGps();
        buildGoogleApiClient();
        lastKnownLocation = false;
        inProgress = true;
        if (canGetLocation())
            mGoogleApiClient.connect();
    }
    
    /**
      Checks FusedLocation's last known location and checks to see if the the time the location was sampled
      and the accuracy of the sample is within specified limits. 
      Otherwise, it sets up fusedlocation updates and tries to get a fresh gps sample.
      @param diffTime - The maximum time, in milliseconds, that may have elapsed since the last sample.
      @param minAccuracy - The minimum accuracy, in meters, that is required of the last sample.
    */
    
    public void getLastKnownLocation(long diffTime, float minAccuracy) {
        this.diffTime = diffTime;
        this.minAccuracy = minAccuracy;
        chooseNetworkGps();
        buildGoogleApiClient();
        lastKnownLocation = true;
        inProgress = true;
        if (canGetLocation())
            mGoogleApiClient.connect();
    }

    public void setCallback(Callback callback) {
        mCallback = callback;
    }

    protected synchronized void buildGoogleApiClient() {
        Log.i(TAG, "Building GoogleApiClient");
        mGoogleApiClient = new GoogleApiClient.Builder(mContext)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
        createLocationRequest();
    }

    protected void createLocationRequest() {
        mLocationRequest = new LocationRequest();

        // Sets the desired interval for active location updates. This interval is
        // inexact. You may not receive updates at all if no location sources are available, or
        // you may receive them slower than requested. You may also receive updates faster than
        // requested if other applications are requesting location at a faster interval.
        mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);

        // Sets the fastest rate for active location updates. This interval is exact, and your
        // application will never receive updates faster than this value.
        mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);

        mLocationRequest.setPriority(PRIORITY);
    }

    /**
     * Requests location updates from the FusedLocationApi.
     */
    public void startLocationUpdates() {
        // The final argument to {@code requestLocationUpdates()} is a LocationListener
        // (http://developer.android.com/reference/com/google/android/gms/location/LocationListener.html).

        LocationServices.FusedLocationApi.requestLocationUpdates(
                mGoogleApiClient, mLocationRequest, this);
    }

    /**
     * Removes location updates from the FusedLocationApi.
     */
    public void stopLocationUpdates() {
        // It is a good practice to remove location requests when the activity is in a paused or
        // stopped state. Doing so helps battery performance and is especially
        // recommended in applications that request frequent location updates.

        // The final argument to {@code requestLocationUpdates()} is a LocationListener
        // (http://developer.android.com/reference/com/google/android/gms/location/LocationListener.html).
        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);

        reset();
    }

    /**
     * Callback that fires when the location changes.
     */
    @Override
    public void onLocationChanged(Location location) {
        numTries++;
        if (mCurrentLocation == null)
            mCurrentLocation = location;
        else if (mCurrentLocation.getAccuracy() > location.getAccuracy()) {
            mCurrentLocation = location;
        }
        if (numTries >= maxTries) {
            mCallback.onLocationResult(mCurrentLocation);
            stopLocationUpdates();
        } else {
            chooseNetworkGps();
            //Toast.makeText(mContext, "latitude: "+mCurrentLocation.getLatitude()+" Longitude: "+mCurrentLocation.getLongitude(), Toast.LENGTH_SHORT).show();
        }

    }

    public Location getLocation(int maxtries) {
        if (numTries >= maxtries)
            return mCurrentLocation;
        else
            return null;
    }

    /**
     * Runs when a GoogleApiClient object successfully connects.
     */
    @Override
    public void onConnected(Bundle connectionHint) {
        Log.i(TAG, "Connected to GoogleApiClient");

        // If the initial location was never previously requested, we use
        // FusedLocationApi.getLastKnownLocation() to get it. If it was previously requested, we store
        // its value in the Bundle and check for it in onCreate(). We
        // do not request it again unless the user specifically requests location updates by pressing
        // the Start Updates button.
        //
        if (lastKnownLocation) {
            mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
            if (mCurrentLocation != null && (mCurrentLocation.getTime() - Calendar.getInstance().getTime().getTime()) < diffTime
                    && mCurrentLocation.getAccuracy() <= minAccuracy) {
                mCallback.onLocationResult(mCurrentLocation);
                reset();
            } else {
                startLocationUpdates();
            }
        } else {
            startLocationUpdates();
        }

    }

    @Override
    public void onConnectionSuspended(int cause) {
        // The connection to Google Play services was lost for some reason. We call connect() to
        // attempt to re-establish the connection.
        Log.i(TAG, "Connection suspended");
        mGoogleApiClient.connect();
    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        // Refer to the javadoc for ConnectionResult to see what error codes might be returned in
        // onConnectionFailed.
        Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + result.getErrorCode());
    }

    /**
     * Function to show settings alert dialog
     * On pressing Settings button will lauch Settings Options
     */
    public void showSettingsAlert() {
        if(!(mContext instanceof Activity)){
            return; //only show dialog if called from activity.
        }
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

        // Setting Dialog Title
        alertDialog.setTitle("GPS settings");

        // Setting Dialog Message
        alertDialog.setMessage("GPS is not enabled. " +
                "This app uses GPS. Do you want to go to settings menu?");

        // On pressing Settings button
        alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                mContext.startActivity(intent);
            }
        });

        // on pressing cancel button
        alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });

        // Showing Alert Message
        alertDialog.show();
    }

    public void reset() {
        numTries = 0;
        mCurrentLocation = null;
        inProgress = false;
        lastKnownLocation = false;
        mGoogleApiClient.disconnect();
    }

    public boolean canGetLocation() {
        return isNetworkEnabled() || isGPSEnabled();
    }

    public boolean isNetworkEnabled() {
        return ((LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE)).isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    }

    public boolean isGPSEnabled() {
        return ((LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE)).isProviderEnabled(LocationManager.GPS_PROVIDER);
    }

    private void chooseNetworkGps() {
        if (isGPSEnabled()) {
            PRIORITY = LocationRequest.PRIORITY_HIGH_ACCURACY;
        } else if (isNetworkEnabled()) {
            PRIORITY = LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY;
        } else {
            PRIORITY = LocationRequest.PRIORITY_NO_POWER;
        }
    }

    public boolean isInProgress() {
        return inProgress;
    }

    interface Callback {
        public void onLocationResult(Location location);
    }
}

2
  • 1
    si consideras que está es la respuesta que resuelve la duda, mejorala agregando más información, no solo links, ya que con el tiempo estos tienden a desaparecer. Tambien tu respuesta es suceptible a ser reportada como de bajacalidad por las mismas razones. Saludos
    – Rene Limon
    Commented el 29 sept. 2017 a las 17:35
  • 1
    Gracias por el comentario. se ha corregido Commented el 29 sept. 2017 a las 19:16
-1

Actualmente es recomendado usar fused location provider que tiene como características principales alta precisión o baja potencia, sin duda es lo que debes usar.

Para obtener la última localización conocida que obtuvo tu provider debes usar el método getLastLocation()

Revisa la documentación:

mFusedLocationClient.getLastLocation()
        .addOnSuccessListener(this, new OnSuccessListener<Location>() {
            @Override
            public void onSuccess(Location location) {
                // La última localización conocida. En algunas situaciones raras esto puede ser nulo.
                if (location != null) {
                    // Logic to handle location object
                }
            }
        });

Anteriormente se usaba getLastKnownLocation pero a diferencia de Fused Location consumía más recursos, principalmente "drenaba" la batería si la implementación no se realizaba correctamente:

LocationManager locManager = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
Location ultimaLocalizacion = locManager.getLastKnownLocation(provider);
3
  • Estoy revisando la documentación y parece que es lo que en realidad necesito. tengo una duda! ¿como le coloco intervalo de tiempo para la ejecución cada 10 minutos? locationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 5000, 0, listener); ¿estoy errado? Commented el 29 sept. 2017 a las 14:24
  • Hey porque -1! Bueno, el segundo parametro es el tiempo entre actualizaciones en este caso sería 600000 milisegundos, en lugar de 5000 sería 600000 revisa esta pregunta es.stackoverflow.com/questions/25928/…
    – Jorgesys
    Commented el 3 oct. 2017 a las 12:23
  • Tienes razón, en la descripción es solo un ejemplo faltó hacer los cálculos en milisegundos Commented el 3 oct. 2017 a las 12:37

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.