I want check my current latitude and longitude. This code is working at the emulator but when i check at phone i have null.(android 10)
The application on the phone has location permission.
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
fusedLocationClient.getLastLocation().addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if(location!=null){
MyLong=location.getLongitude();
MyLat=location.getLatitude();
test.setText(MyLat+" " +MyLong);
}
else{
test.setText("null");
}
}
});
Based on the official android documentation, there are some situations that the last known location could be null and those are
In those situations you should request location updates. You could check the example of Google's Codelabs