Android 从方法返回一个值
我使用此方法获取用户位置,并希望返回myLocation变量:Android 从方法返回一个值,android,methods,Android,Methods,我使用此方法获取用户位置,并希望返回myLocation变量: mFusedLocationClient.getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() { @Override public void onSuccess(Location location) { // Got last known location. In some
mFusedLocationClient.getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
// Got last known location. In some rare situations this can be null.
if (location != null) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
LatLng mylocation = new LatLng(latitude, longitude);
}
}
});
我在一个返回LatLng值的函数中使用了这个方法,但我无法从该方法中获取值。声明Fused Location客户端类
private Location currentLocation;
public Location getCurrentLocation() {
return currentLocation;
}
mFusedLocationClient.getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
// Got last known location. In some rare situations this can be null.
if (location != null) {
this.currentLocation= location;
}
}
});
在此处键入完整的方法。我要记住的是,位置是一个服务,您必须根据回调处理代码events@AbuQauod因此,完整的方法已经是广播接收器或总线。@SaurabhVadhvaI不能使用它,因为我需要位置,然后我调用函数。你不能更具体一点吗?
if (getCurrentLocation() != null) {
double latitude = getCurrentLocation() .getLatitude();
double longitude = getCurrentLocation() .getLongitude();
LatLng mylocation = new LatLng(latitude, longitude);
}