从方法

时间:2018-03-19 10:05:24

标签: android methods

我有这个方法来获取用户位置,我想返回myLocation变量:

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值的函数中有这个方法,但我无法从方法内部获取值。

1 个答案:

答案 0 :(得分:-1)

声明Fused Location Client类

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;
            }
        }
    });

在getCurrentLocation()之后处理当前位置LatLng

        if (getCurrentLocation()  != null) {
            double latitude = getCurrentLocation() .getLatitude();
            double longitude = getCurrentLocation() .getLongitude();

            LatLng mylocation = new LatLng(latitude, longitude);
        }
相关问题