LatLng变量未分配其纬度和经度

问题描述 投票:0回答:1

我目前在我的android代码方面遇到问题。我的LatLng变量currentLocation始终为null,我似乎无法对其进行设置,以便将其分配给在onSuccess()侦听器中获得的两个纬度和经度值。

在方法getCurrentLocation()中,我从Task中获得了纬度和经度值,然后将其放入变量current中,然后为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) {
...
    }
}

提前谢谢您

android maps
1个回答
0
投票

我实现了两个方法之间的回调,所以当getCurrentLocation完成时,然后初始化onMapReady逻辑:

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();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.