gpt4 book ai didi

java - 为什么即使 myLocation 是全局变量,当 onSuccess() 终止时 myLocation 也会变为 null?

转载 作者:行者123 更新时间:2023-12-02 10:17:14 26 4
gpt4 key购买 nike

当 onSuccess() 方法终止时,myLocation 值变为 null。我是 Android 应用程序开发新手。我想将当前位置的值永久存储在 myLocation 中。

我正在按照谷歌的开发人员指南获取我的当前位置,并且我想将其存储到 myLocation(全局变量)。我已成功获取当前位置,但它并未永久存储在 myLocation 中。在 onSuccess() 方法之后,myLocation 变为 null。

公共(public)类 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=?!?等=?!?]请帮助我并提前致谢。

最佳答案

日志在 onSuccess() 之前被调用,因为它是一个Listener。你应该做的是调用

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

在你的onSuccess()中,之后你就得到了正确的值。

关于java - 为什么即使 myLocation 是全局变量,当 onSuccess() 终止时 myLocation 也会变为 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54597878/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com