大家好,我现在遇到一个问题。当我尝试从下面的方法返回两个值以应用到另一个变量时,它总是返回0.0。任何人都可以帮我弄清楚我的问题是什么。提前致谢。这是我的代码
public double[] getLocation() {
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
}else{
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null){
double latti = location.getLatitude();
double longi = location.getLongitude();
this.latti = latti;
this.longi = longi;
}
}
return new double[]{latti,longi};
}
这是我的方法,我想在这里应用这两个值
Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode){
case REQUEST_LOCATION:getLocation();
break;
}
}
这是onRequestionPermission方法
我剩下的代码
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
double latti;
double longi;
LocationManager locationManager;
static final int REQUEST_LOCATION = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
double[] curLocation = getLocation();
double latitude = curLocation[0];
double longtitude = curLocation[1];
Toast.makeText(this, "latitude: " + latitude + "longitude: " + longtitude, Toast.LENGTH_SHORT).show();
}
但似乎总是返回0.0和0.0 ......
答案 0 :(得分:0)
覆盖JSONObject
并检查是否已授予权限,如果是,请在此处移动其他代码。
onRequestPermissionsResult()