androidandroid-studiolocationfusedlocationproviderclient

location data null with .getLastLocation() FusedLocationProviderClient


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!


Solution

  • 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