before get last location, maybe you want get current location, check if null or have. if not set last loccation if you want use FusedLocationProviderClient and before use it adding this:
implementation 'com.google.android.gms:play-services-location:16.0.0'
on your build.gradle(Module: app), and call method zoomMyCuurentLocation() whean activity create.
private void zoomMyCuurentLocation() {
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION);
}
Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
if (location != null) {
double lat = location.getLatitude();
double longi = location.getLongitude();
LatLng latLng = new LatLng(lat,longi);
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 14.f));
Log.d(TAG, "zoomMyCuurentLocation: location not null");
} else {
setMyLastLocation();
}
}
private void setMyLastLocation() {
Log.d(TAG, "setMyLastLocation: excecute, and get last location");
FusedLocationProviderClient fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
fusedLocationClient.getLastLocation().addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null){
double lat = location.getLatitude();
double longi = location.getLongitude();
LatLng latLng = new LatLng(lat,longi);
Log.d(TAG, "MyLastLocation coordinat :"+latLng);
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 14.f));
}
}
});
}
Answer from rivaldy on Stack Overflowgeolocation - How to get last known location for Location manager in Android? - Stack Overflow
How do i get my last known location in android? - Stack Overflow
How to view last locations on Google Find My Device ? - Android Community
android - How to get last location of a device? - Stack Overflow
Videos
before get last location, maybe you want get current location, check if null or have. if not set last loccation if you want use FusedLocationProviderClient and before use it adding this:
implementation 'com.google.android.gms:play-services-location:16.0.0'
on your build.gradle(Module: app), and call method zoomMyCuurentLocation() whean activity create.
private void zoomMyCuurentLocation() {
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION);
}
Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
if (location != null) {
double lat = location.getLatitude();
double longi = location.getLongitude();
LatLng latLng = new LatLng(lat,longi);
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 14.f));
Log.d(TAG, "zoomMyCuurentLocation: location not null");
} else {
setMyLastLocation();
}
}
private void setMyLastLocation() {
Log.d(TAG, "setMyLastLocation: excecute, and get last location");
FusedLocationProviderClient fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
fusedLocationClient.getLastLocation().addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null){
double lat = location.getLatitude();
double longi = location.getLongitude();
LatLng latLng = new LatLng(lat,longi);
Log.d(TAG, "MyLastLocation coordinat :"+latLng);
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 14.f));
}
}
});
}
Silly thing as answer to this ...I restarted my device ...and it worked...Please ensure that your service for location are enabled and working properly
GetLastLocation is called by the FusedLocationApi and stores the last location based on the last client that had used it. If your app is the first client to use it, it probably will not have gotten the last location as quickly as you call your "OnConnected()" function.
Here you have some potential options:
1. Create A Checking Thread
You could implement a thread that checks for the last known location every X amount of seconds. Then, regardless of whether or not your app was the first to launch, you will eventually receive the last location.
2. Implement Location Listening
You could also implement Location Listening in your activity and update your location every time the OnLocationChanged event is fired.
First onConnected does not get called immediately. It takes some time depending on various factors. So keep a log statement and check when onConnected is called.
Also if the device location is turned off then you will never receive any data back. It will be null.
Also it is not necessary that the google api client will hold your last location irrespective of whether you are connected to internet or not. It might and it might not. It only caches the last location and cache can be cleared anytime by the android OS.
Fused Location Provider Client is the best way to do it. Try with this code for the getLastLocation() method:
FusedLocationProviderClient mLocationProvider = LocationServices.getFusedLocationProviderClient(activity);
mLocationProvider.getLastLocation().addOnSuccessListener(activity, location -> {
if (location != null) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
}
}).addOnFailureListener(e -> {
//some log here
});
Can you try below solution and let me know, because it's working fine for me.
Change your provider as
String provider = LocationManager.NETWORK_PROVIDER;
You have mentioned this provider in getLastLocation() method.
Also add below permissions to your manifest files
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.BIND_TELECOM_CONNECTION_SERVICE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>