How can I get the user's location every second?

0

I am developing an Android application where I want to get the location as quickly as possible (every second would be fine).

For now I have added the Google Play Location services as I have seen in many tutorials and I get the correct location of the device.

The problem is that in the code:

locationRequest.setFastestInterval(1000);
locationRequest.setPriority(500);

It seems that it does not do anything because I get the updates of the location every 10-15 seconds.

I add the whole code:

 public class MainActivity extends AppCompatActivity {

    private FusedLocationProviderClient fusedLocationClient;
    public final long UPDATE_INTERVAL_IN_MILLISECONDS = 1000;
    public final long UPDATE_FASTEST_INTERVAL_IN_MILLISECONDS = UPDATE_INTERVAL_IN_MILLISECONDS / 2;

    final int LOCATION_REQUEST = 1;
    LocationRequest locationRequest;
    LocationCallback locationCallback;

    TextView velocidad;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        velocidad = findViewById(R.id.velocidad);

        fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);

        locationRequest = new LocationRequest();
        locationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
        locationRequest.setFastestInterval(UPDATE_FASTEST_INTERVAL_IN_MILLISECONDS);
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locationRequest.setSmallestDisplacement(0f);

        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
        builder.addLocationRequest(locationRequest);
        LocationSettingsRequest locationSettingsRequest = builder.build();

        SettingsClient settingsClient = LocationServices.getSettingsClient(this);
        settingsClient.checkLocationSettings(locationSettingsRequest);

        locationCallback = new LocationCallback() {
            @Override
            public void onLocationResult(LocationResult locationResult) {
                if (locationResult == null) {
                    return;
                }
                onLocationChanged(locationResult.getLastLocation());
            }
        };
        startLocationUpdates();
    }

    private void startLocationUpdates() {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, LOCATION_REQUEST);
            return;
        }
        fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, Looper.myLooper());
    }

    public void onLocationChanged(Location location) {
        // New location has now been determined
        String msg = "Updated Location: " +
                Double.toString(location.getLatitude()) + "," +
                Double.toString(location.getLongitude()) + " Velocidad: "+location.hasSpeed();
        System.out.println(msg);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){
        startLocationUpdates();
    }
}

The problem is the update time that gives me the locations every 10-15 seconds instead of every second and I can not get the time down, thanks in advance.

    
asked by Jsanzo97 30.11.2018 в 19:52
source

2 answers

1

From my experience when you establish the minimum distance of dsplazamiento:

locationRequest.setSmallestDisplacement(0f);

This takes priority over time:

locationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
locationRequest.setFastestInterval(UPDATE_FASTEST_INTERVAL_IN_MILLISECONDS);

Try to remove

locationRequest.setSmallestDisplacement(0f);
    
answered by 30.11.2018 в 20:36
1

When defining the time interval between each request, if you want to do it every second, you would define this value in milliseconds, it would be:

UPDATE_INTERVAL_IN_MILLISECONDS = 1000; //1 segundo
locationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
 //locationRequest.setFastestInterval(UPDATE_FASTEST_INTERVAL_IN_MILL ISECONDS);

But this is not recommended as it will mainly have a high consumption of the battery.

    
answered by 01.12.2018 в 00:16