GetLastKnownLocation returns null

I read some questions about this, but I did not find the answer I need.

So the thing is, I have my map installed and I want to capture the current gps location. I checked that my variables are not NULL, but still my result is:

getLastKnownLocation(provider, false); 

Gives me a null value, so I need some help. I have added permissions for the location COARSE + FINE. But I usually have all kinds of network data turned off for my phone, because I'm not very happy with the unpredictable cost of data flow in my phone bill. Therefore, for this test, I only have WiFi turned on and connected.

Is there anything else I need to make this possible? I think WiFi should be enough?

+69
android google-maps location
Dec 07 '13 at 6:54
source share
7 answers

Use this method to get the last known location:

 LocationManager mLocationManager; Location myLocation = getLastKnownLocation(); private Location getLastKnownLocation() { mLocationManager = (LocationManager)getApplicationContext().getSystemService(LOCATION_SERVICE); List<String> providers = mLocationManager.getProviders(true); Location bestLocation = null; for (String provider : providers) { Location l = mLocationManager.getLastKnownLocation(provider); if (l == null) { continue; } if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) { // Found best last known location: %s", l); bestLocation = l; } } return bestLocation; } 
+173
Dec 09 '13 at 8:00
source share

You mix the new and old location APIs together when you shouldn't.

to get the last known location, all you have to do is call

 compile "com.google.android.gms:play-services-location:11.0.1" mLocationClient.getLastLocation(); 

After the location service has been connected.

read how to use the new location API

http://developer.android.com/training/location/retrieve-current.html#GetLocation

  private void getLastLocationNewMethod(){ FusedLocationProviderClient mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this); mFusedLocationClient.getLastLocation() .addOnSuccessListener(new OnSuccessListener<Location>() { @Override public void onSuccess(Location location) { // GPS location can be null if GPS is switched off if (location != null) { getAddress(location); } } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { Log.d("MapDemoActivity", "Error trying to get last GPS location"); e.printStackTrace(); } }); } 
+24
Dec 30 '17 at 11:49
source share

You are trying to get a cached location from a network provider. You have to wait a few minutes until you get the right solution. Since the network provider's cache is empty, you obviously get zero.

+5
Dec 07 '13 at 6:55
source share

Try this, it works well for me: -

 LocationManager mLocationManager; Location myLocation = getLastKnownLocation(); private Location getLastKnownLocation() { Location oldLoc; while (true){ oldLoc = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); if (oldLoc == null){ continue; } else { break; } } return oldLoc; } 

You can use NETWORK_PROVIDER instead of GPS_PROVIDER for faster results.

+1
Apr 11 '19 at 19:52
source share

Add this code

 if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return; } 

before function:

locationManagerObject.requestLocationUpdates(LocationManager.GPS_PROVIDER, MINIMUM_TIME_BETWEEN_UPDATES, MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, new LocationListener()) as well as the function: locationManagerObject.getLastKnownLocation(LocationManager.GPS_PROVIDER)

0
Sep 20 '16 at
source share

feel free to use my simple class and it works like a charm and you can design it for complex GPS projects

 public class GPS extends LocationListner{ Context mContext; boolean isGPSEnabled; boolean canGetLocation; boolean isPassiveEnabled; boolean isNetworkEnabled; Location location; LocationManager locationManager; public GPS(Context c) { this.mContext = c; } public Location getLocation() { int MIN_TIME_BW_UPDATES = 10000; int MIN_DISTANCE_CHANGE_FOR_UPDATES = 10000; if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { Toast.makeText(mContext, "PLZ open up GPS To Access", Toast.LENGTH_SHORT).show(); return null; } try { locationManager = (LocationManager) getApplicationContext() .getSystemService(LOCATION_SERVICE); isGPSEnabled = locationManager .isProviderEnabled(LocationManager.GPS_PROVIDER); isPassiveEnabled = locationManager .isProviderEnabled(LocationManager.PASSIVE_PROVIDER); isNetworkEnabled = locationManager .isProviderEnabled(LocationManager.NETWORK_PROVIDER); if (isGPSEnabled || isNetworkEnabled || isPassiveEnabled) { this.canGetLocation = true; // if GPS Enabled get lat/long using GPS Services if (isGPSEnabled && location == null) { locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); Log.d("GPS", "GPS Enabled"); if (locationManager != null) { location = locationManager .getLastKnownLocation(LocationManager.GPS_PROVIDER); } } if (isPassiveEnabled && location == null) { locationManager.requestLocationUpdates( LocationManager.PASSIVE_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); Log.d("Network", "Network Enabled"); if (locationManager != null) { location = locationManager .getLastKnownLocation(LocationManager.PASSIVE_PROVIDER); } } if (isNetworkEnabled && location == null) { locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); Log.d("Network", "Network Enabled"); if (locationManager != null) { location = locationManager .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); } } } else { return null; } } catch (Exception e) { e.printStackTrace(); } return location; } @Override public void onLocationChanged(Location location) { } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } 

}

0
Nov 12 '17 at 8:52
source share

Replace ur string with

 Location location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient); 
-four
Jun 16 '17 at 11:05
source share



All Articles