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;
}
}
}