Android LatLng变量未指定其纬度和经度

Android LatLng变量未指定其纬度和经度,android,maps,Android,Maps,我目前的android代码有问题。我的LatLng变量currentLocation始终为null,我似乎无法将其设置为指定给在onSuccess()侦听器中获得的两个纬度和经度值 在getCurrentLocation()方法中,我从任务中获取纬度和经度值,然后将其放入变量currentI中,然后分配currentLocation,即current的值。我使用了断点,但当前未分配给currentLocation 请参阅参考代码: public class MapsActivity extend

我目前的android代码有问题。我的LatLng变量currentLocation始终为null,我似乎无法将其设置为指定给在onSuccess()侦听器中获得的两个纬度和经度值

在getCurrentLocation()方法中,我从任务中获取纬度和经度值,然后将其放入变量
current
I中,然后分配currentLocation,即current的值。我使用了断点,但当前未分配给currentLocation

请参阅参考代码:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;
public Location myLocation;
public LatLng currentLocation;
public double latitude;
public double longitude;
FusedLocationProviderClient fusedLocationProviderClient;
private static final int REQUEST_CODE = 101;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
    currentLocation = new LatLng(0,0);

    fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);

}


private void getCurrentLocation() {
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE);
        return;
    }

    Task<Location> task = fusedLocationProviderClient.getLastLocation();
    task.addOnSuccessListener(new OnSuccessListener<Location>() {
        @Override
        public void onSuccess(Location location) {
            if(location != null){
               myLocation = location;

                double lat = location.getLatitude();
                double lon = location.getLongitude();
                Log.v("MapsActivity", "Lat: " + latitude + "    Long: " + longitude);

                latitude = lat;
                longitude = lon;
                LatLng current = new LatLng(latitude, longitude);
                currentLocation = current;
            }
        }
    });

}


@Override
public void onMapReady(GoogleMap googleMap) {
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE);
        return;
    }
    getCurrentLocation();
    Log.v("MapsActivity", "Latitude: " + latitude + "    Longitude: " + longitude);
    mMap = googleMap;

    mMap.addMarker(new MarkerOptions().position(currentLocation).title("Your location"));

    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(currentLocation,10));



}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
...
    }
}
公共类MapsActivity扩展了FragmentActivity在MapreadyCallback上的实现{
私有谷歌地图;
公共场所;
公共停车位;
公共双纬度;
公共双经度;
FusedLocationProviderClient FusedLocationProviderClient;
私有静态最终整数请求_代码=101;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_映射);
//获取SupportMapFragment,并在地图准备好使用时收到通知。
SupportMapFragment mapFragment=(SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map);
getMapAsync(这个);
当前位置=新车床(0,0);
fusedLocationProviderClient=LocationServices.getFusedLocationProviderClient(此);
}
私有void getCurrentLocation(){
if(ActivityCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS\u FINE\u LOCATION)!=PackageManager.permission\u已授予和&ActivityCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS\u LOCATION)!=PackageManager.permission\u已授予){
ActivityCompat.requestPermissions(这是一个新字符串[]{Manifest.permission.ACCESS\u FINE\u LOCATION},请求代码);
返回;
}
Task Task=fusedLocationProviderClient.getLastLocation();
task.addOnSuccessListener(新的OnSuccessListener(){
@凌驾
成功时的公共无效(位置){
如果(位置!=null){
myLocation=位置;
双纬度=location.getLatitude();
double lon=location.getLongitude();
Log.v(“地图活动度”,“纬度:“+纬度+”长:“+经度”);
纬度=纬度;
经度=经度;
LatLng当前=新LatLng(纬度、经度);
当前位置=当前位置;
}
}
});
}
@凌驾
4月1日公开作废(谷歌地图谷歌地图){
if(ActivityCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS\u FINE\u LOCATION)!=PackageManager.permission\u已授予和&ActivityCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS\u LOCATION)!=PackageManager.permission\u已授予){
ActivityCompat.requestPermissions(这是一个新字符串[]{Manifest.permission.ACCESS\u FINE\u LOCATION},请求代码);
返回;
}
getCurrentLocation();
Log.v(“地图活性”,“纬度:“+纬度+”经度:“+经度”);
mMap=谷歌地图;
mMap.addMarker(新的MarkerOptions().position(当前位置).title(“您的位置”);
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(当前位置,10));
}
@凌驾
public void onRequestPermissionsResult(int-requestCode,@NonNull-String[]permissions,@NonNull-int[]grantResults){
...
}
}

提前感谢

我实现了两个方法之间的回调,因此当getCurrentLocation完成时,初始化OnMaReady逻辑:

class ExampleCallbackClass {

    interface Callback {
        void onFinish();
    }

    void onMapReady() {
        Callback callback = new Callback() {
            @Override
            public void onFinish() {
                // Your onMapReady logic
            }
        };

        getCurrentLocation(callback);
    }


       private void getCurrentLocation(Callback callback) {
        // Your logic, and finally:
        callback.onFinish();
    }
}

问题在于getCurrentLocation()方法是异步的,当执行onMapReady()时,结果尚未初始化。一个解决方案是使用回调,因此当onFusedLocation完成时,您可以更新googleMap变量我将如何实现回调?(正如《守则》中所说,我是新来的)