为什么myLocation成为空当的onSuccess(),即使myLocation是一个全局变量被终止?

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

当该方法的onSuccess()终止myLocation值变为零。我是新来的Android应用程序开发。我想存储currentlocation在myLocation值永久。

我下面谷歌的开发人员指南,让我currentlocation,我想将其保存至myLocation(全局变量)。我已经成功地获得了我的currentlocation但没有得到存储在myLocation永久。的onSuccess()方法之后的myLocation变为零。

公共类MapsActivity扩展FragmentActivity实现OnMapReadyCallback {

private GoogleMap mMap;
private FusedLocationProviderClient mFusedLocationClient;
SupportMapFragment mapFragment;
Location myLocation;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    myLocation = new Location(LocationManager.NETWORK_PROVIDER);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
     mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);

    mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ) {

        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
    }
    else{
        mapFragment.getMapAsync(this);

    }
}



@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Sydney and move the camera
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ) {

        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
    }

    mFusedLocationClient.getLastLocation()
            .addOnSuccessListener(this, new OnSuccessListener<Location>() {
                @Override
                public void onSuccess(Location currentlocation) {
                    myLocation=currentlocation;
                    // Got last known location. In some rare situations this can be null.
                    if (currentlocation != null) {

                        LatLng sydney = new LatLng(myLocation.getLatitude(), myLocation.getLongitude());
                        mMap.addMarker(new MarkerOptions().position(sydney).title("My Location"));
                        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));


                    }
                }
            });
    Log.i("myLocation",myLocation.toString());


}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    if (requestCode == 1) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            mapFragment.getMapAsync(this);
        } else {
            Toast.makeText(getApplicationContext(), "Please provide permission", Toast.LENGTH_SHORT).show();
        }
    }


}

}

myLocation应该等于currentlocation但它被发现是在日志空..原木 - I / myLocation:位置[网络0.000000,0.000000 ACC = ??? T =?!?等=?!?]请帮助我,在此先感谢。

java android location
1个回答
1
投票

日志让您onSuccess()之前调用,因为它是一个Listener。你应该做的就是把呼吁

Log.i("myLocation",myLocation.toString());

onSuccess(),以后你有正确的价值。

© www.soinside.com 2019 - 2024. All rights reserved.