1

I have successfully used FusedLocation to get devices last known location before, but I am uncertain why it isn't working in this context.

I have tested it on a physical device, as well as an emulated device.

I implemented play-services, and stated the required permissions in the manifest. I have also checked device permissions for my app, enabled location, and have used Google Maps insure there is a last known location.

I declareprivate FusedLocationProviderClient fusedLocationClient; in my MainActivity Class.

In Main Activity onCreate(): FusedLocationClient = LocationServices.getFusedLocationProviderClient(this);

Now I am trying to retrieve the last known location from when the user selects an item from an options menu.

if(id == R.id.action_GPS){

        fusedLocationClient.getLastLocation().addOnSuccessListener(this, new OnSuccessListener<Location>() {
            @Override
            public void onSuccess(Location location) {
                if(location != null) {
                    Geocoder gcd = new Geocoder(context, Locale.getDefault());
                    try {
                        List<Address> adrs = gcd.getFromLocation(location.getLatitude(), location.getLongitude(), 1);

                    } catch (IOException e) {

                    }
                }

After debugging over 'fusedLocationClient.getLastLocation()..." it skips to the end, and never reaches the onSuccess method. Why could this be failing?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • I'd take Cường Nguyễn's advice and add some logging even if location is null. Then you might be able to debug the problem a bit more. I actually had similar problems when I never requested location updates myself. – JensV Dec 20 '19 at 07:43
  • Does this answer your question? [fusedLocationProviderClient.lastLocation.addOnSuccessListener always null](https://stackoverflow.com/questions/50047863/fusedlocationproviderclient-lastlocation-addonsuccesslistener-always-null) – JensV Dec 20 '19 at 07:44
  • I ended up just making location requests. Don't really see the point of FusedLocation considering the inconsistencies of it – mpetersen910 Dec 21 '19 at 02:03

2 Answers2

2

The location object may be null in the following situations:

  • Location is turned off in the device settings. The result could be
    null even if the last location was previously retrieved because disabling location also clears the cache.
  • The device never recorded its location, which could be the case of a new device or a device that has been restored to factory settings.
  • Google Play services on the device has restarted, and there is no active Fused Location Provider client that has requested location after the services restarted. To avoid this situation you can create a new client and request location updates yourself.

You should requestLocationUpdates when lastLocation == null

Cuong Nguyen
  • 970
  • 6
  • 17
0

you can check if GPS is active using

val client = LocationServices.getSettingsClient(this)
        val task = client.checkLocationSettings(builder.build())
        task.addOnSuccessListener {
            //GPS is ON
        }
        task.addOnFailureListener {
            if (it is ResolvableApiException) {
                try {
                       //GPS is OFF
                    Toast.makeText(this, "Mohon aktifkan GPS anda",
                            Toast.LENGTH_LONG).show()
                } catch (sendEx: IntentSender.SendIntentException) {
                    // Ignore the error.
                }
            }
        }

and dont forget to check user's permission

adi purnama
  • 156
  • 2
  • 6