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.