I am using simple location manager object to get lastKnownLocation() of device but getting null object in return can any one tell me why ?
Code :
public Location getLocation() {
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
if (locationManager != null) {
Location lastKnownLocationGPS = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (lastKnownLocationGPS != null) {
return lastKnownLocationGPS;
} else {
Location loc = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
System.out.println("1::"+loc);----getting null over here
System.out.println("2::"+loc.getLatitude());
return loc;
}
} else {
return null;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.location);
getLocation();-----calling service
}
permissions given :
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
so is anything missing to set up? I have checked my location service is on in device please give some links for working examples
Track your phone's location using Google MapsGo to http://android.com/find. Sign in with your Gmail™ account and password. On the map, you'll see your phone's approximate location. If the device cannot be found, it'll show you the last known location (if available).
Under the section, Family Watch, you will see that the Locations option is turned on by default. It should be turned on to get the feature work properly. Tap the 'back arrow' sign to navigate back to Dashboard.
First, create a Location Manager object by calling the getSystemService() method and LOCATION_SERVICE as an argument. Call getBestProvider() to get the location in the form of a string. Now the provider to getLastKnownLocation() as an argument to get the location.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With