I'm trying to get a user's last known location with Android. I can do it using fused locations, but I'm trying to make a simple method to return a user's latitude and longitude location.
Here is what I've tried:
public static double[] getLocation (Context c) {
final double[] returnLocation = {0, 0};
FusedLocationProviderClient fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(c);
if (c.getApplicationContext().checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
fusedLocationProviderClient.getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null) {
returnLocation[0] = location.getLatitude();
returnLocation[1] = location.getLongitude();
}
}
});
}
return returnLocation;
}
However, I'm afraid that this is asynchronious, so it returns without actually getting the location.
I appreciate the help.