location data null with .getLastLocation() FusedLocationProviderClient

Fab*_*oni 6 android location android-studio 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();
            }
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

In my button handler I call getLocation() and use latitude, longitude and accuracy to store to the database.

Any help appreciated!

ten*_*int 12

当 getLastLocation() 为 null 时,需要做一个 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())
Run Code Online (Sandbox Code Playgroud)

如果您不需要持续更新,您可以在收到请求后将其删除。

mFusedLocationClient.removeLocationUpdates(locationCallback);
Run Code Online (Sandbox Code Playgroud)

更多信息在这里:https : //medium.com/@droidbyme/get-current-location-using-fusedlocationproviderclient-in-android-cb7ebf5ab88e


Tha*_*mim 6

当手机重新启动时,缓存的最后一个位置会丢失,所以如果你没有打开一个使用 GPS 的应用程序,比如谷歌地图或其他东西,那么将没有最后一个位置。

永远不必返回给您的位置,您应该始终假设它可能为空。

如果你想获取位置,你只需要像@tenprint 在这个线程中所说的那样使用 LocationCallback 。

参考这个链接

手机重启后Android获取位置为空