方法中的值返回0.0

时间:2018-03-13 06:40:06

标签: java android

大家好,我现在遇到一个问题。当我尝试从下面的方法返回两个值以应用到另一个变量时,它总是返回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 ......

1 个答案:

答案 0 :(得分:0)

覆盖JSONObject并检查是否已授予权限,如果是,请在此处移动其他代码。

onRequestPermissionsResult()