3

I know this has been talked about many times but nothing has helped me fix the problem that getLastKnownLocation is returning null.

My Activity implement LocationListener:

public class MainActivity extends Activity implements
    GooglePlayServicesClient.ConnectionCallbacks,
    GooglePlayServicesClient.OnConnectionFailedListener, LocationListener {

I have this in my onCreate method:

  LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
  lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

And i then try to get the user location like so (also in onCreate):

    // Getting the Co-ordinates of the users current location
    Location location = lm
            .getLastKnownLocation(LocationManager.GPS_PROVIDER);

    if(location != null){
        longitude = location.getLongitude();
        latitude = location.getLatitude();
    }

I tried then:

@Override
public void onLocationChanged(Location location) {      
    longitude = location.getLongitude();
    latitude = location.getLatitude();
}

Am I missing something?

EDIT: Still not working but code I have tried:

    if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
    while (location == null) {
        if (counter > MAX_TIME) {   
        }
        else {
            try {
                 location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            counter += 1000;
        }
    }
    }
Natalie Carr
  • 3,707
  • 3
  • 34
  • 68

2 Answers2

1

You have to wait for a while to get the location because GPS take some seconds to warm up.

If you call directly getLastLocation it will return you last find location or sometime null when there is no cache location. It always good if you implement a location service and try to get the location of onLocationChanged. You can keep this variable as static to access in other classes.

Sometime, GPS provider fail to get location when you are in building so in that case you can switch to Network provider to get the location but in Network provider accurary would be less as comparision to GPS provider

For more accurate location, you can use a while in onLocationChanged until you think accuracy is better.

Ajay S
  • 48,003
  • 27
  • 91
  • 111
1

From documentation:

"Returns a Location indicating the data from the last known location fix obtained from the given provider.

Returns the last known location for the provider, or null "

So, there must be at least one previous location obtained using the supplied provider. For it to receive a location, you have to register for updates at least once using that provider and receive an actual update. After which, getLastKnownLocation will return that value. If you want to register for locations provided by other apps, use Location.PASSIVE_PROVIDER.

Hope this helps.

Edit: As I commented above, put a while loop in that iterates say for 30 seconds and checks every second if the location is not longer null. You need this to happen at least once, to then be able to use getLastKnownLocation, AFAIK.

Edit with small, untested example. Best to put all this in an AsyncTask too i.e. in doInBackground. Note, I'm assuming you are storing the Location object itself, rather than separating it out into longitude and latitude.:

if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
    long final MAX_TIME = 30000;
    long counter = 0;
    while (location == null) {
        if (counter > MAX_TIME) {
            return False;
        }
        Thread.sleep(1000);
        counter += 1000;
    }
} else {
    return False;
}

Edit: Remember, Location is updated in onLocationUpdate(Location location). One this has updated, you then have a location object not equal to null.