What is the best way to get the location?

0

I am doing a "Tracking" app or follow-up, to get the location I use LocationListener within a service, I run the app on the device but sometimes I can notice that the location is somewhat erratic and even sometimes does not receive updates (the coordinates are kept at zero), I've been reading about other methods to obtain the location (FusedLocationProvider), I would like to know if I can do this with LocationListener and the best way to do it or if it is more convenient to use FusedLocationProvider as an alternative. p>

This is the code of my service:

public class LocationService extends Service implements LocationListener {

private final String DEBUG_TAG = "GPSPING";

private LocationManager lm;
private double latitude;
private double longitude;
private double accuracy;
private Location location;
private boolean isEnabled = false;


@Override
public void onLocationChanged(Location location) {
    Log.d(DEBUG_TAG, "onLocationChanged");

    latitude = location.getLatitude();
    longitude = location.getLongitude();
    accuracy = location.getAccuracy();
}

@Override
public void onProviderDisabled(String provider) {
    Log.d(DEBUG_TAG, "onProviderDisabled");
}

@Override
public void onProviderEnabled(String provider) {
    Log.d(DEBUG_TAG, "onProviderEnabled");
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10f, this);
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    Log.d(DEBUG_TAG, "onStatusChanged");

}

@Override
public void onCreate() {
    Log.d(DEBUG_TAG, "onCreate");

}

@Override
public void onDestroy() {
    lm.removeUpdates(this);
    Log.d(DEBUG_TAG, "onDestroy");


}

@Override
public IBinder onBind(Intent intent) {
    Log.d(DEBUG_TAG, "onBind");

    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return super.onStartCommand(intent, flags, startId);
}

@Override
public void onStart(final Intent intent, int startid) {
    Log.d(DEBUG_TAG, "onStart");

    Location location = getLocation();
    latitude = location.getLatitude();
    longitude = location.getLongitude();

    Log.d(DEBUG_TAG, "Location Lat:"+latitude +"Long: "+longitude);

}



public Location getLocation() {

    try {
        lm = (LocationManager) this.getSystemService(LOCATION_SERVICE);


        boolean isGPSEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);


        boolean isNetworkEnabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
        } else {
            if (isNetworkEnabled) {
                lm.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        1000,
                        10f, this);
                Log.d("Network", "Network Enabled");
                if (lm != null) {
                    location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }

            if (isGPSEnabled) {
                if (location == null) {
                    lm.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            1000,
                            10f, this);
                    Log.d("GPS", "GPS Enabled");
                    if (lm != null) {
                        location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
}
}
    
asked by Aarón Zúñiga 06.06.2017 в 04:56
source

1 answer

0

I used GoogleApiClient

You will simply have to implement GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener in the class where you want to implement it.

With these interfaces you will have to implement the methods:

@Override
    public void onConnected(@Nullable Bundle bundle) {

    //Esto es para implementar un intervalo para recibir las localizaciones y la prioridad

    LocationRequest mLocationRequest;

    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(1000);
    mLocationRequest.setFastestInterval(1000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
}

@Override
    public void onConnectionSuspended(int i) {
    //Cuando la conexión se suspende
}

@Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
     //Cuando la conexión falla
    }

@Override
    public void onLocationChanged(Location location) {
    //Se ejecuta cada vez que la localización cambia
}

I have used this and it has worked perfectly for me.

    
answered by 06.06.2017 в 15:16