I am getting coordinates with FusedLocationProviderClient in my application on a button click to store into a database. The issue is that when I reboot the phone or the emulator the .getLastLocation()
is null and I have to click another time the button i order for this to work. Is there a way to force to get a current position if the value of location from .lastKnownPosition()
is null?
// Google fused location client for GPS position
private FusedLocationProviderClient flpc;
// Vars to store GPS info
public Double latitude, longitude;
public Float accuracy;
// Google Fused Client for location
public void getLocation() {
// FusedLocationProviderClient
flpc = LocationServices.getFusedLocationProviderClient(context);
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
......
return;
}
// Get the latest position from device
Task<Location> task = flpc.getLastLocation();
task.addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if(location!=null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
accuracy = location.getAccuracy();
}
}
});
}
In my button handler I call getLocation()
and use latitude, longitude and accuracy to store to the database.
Any help appreciated!
Previously we have taught you how you get current location using GPS/Network Provider. Then android has revealed FusedLocationProviderClient under GoogleApi. FusedLocationProviderClient is for interacting with the location using fused location provider. (NOTE : To use this feature, GPS must be turned on your device.
Track your phone's location using Google MapsGo to http://android.com/find. Sign in with your Gmail™ account and password. On the map, you'll see your phone's approximate location. If the device cannot be found, it'll show you the last known location (if available).
When getLastLocation() is null, you need to make a LocationRequest
private LocationRequest locationRequest;
private LocationCallback locationCallback;
...
locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(20 * 1000);
locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
if (locationResult == null) {
return;
}
for (Location location : locationResult.getLocations()) {
if (location != null) {
wayLatitude = location.getLatitude();
wayLongitude = location.getLongitude();
txtLocation.setText(String.format(Locale.US, "%s -- %s", wayLatitude, wayLongitude));
}
}
}
};
mFusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, Looper.getMainLooper())
If you don't need continuous updates, you can remove the request once you've received it.
mFusedLocationClient.removeLocationUpdates(locationCallback);
More info here: https://medium.com/@droidbyme/get-current-location-using-fusedlocationproviderclient-in-android-cb7ebf5ab88e
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With