When I try to return two values from the method below to apply into another variable it always return 0.0. What's my problem? Here is my code:
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};
}
This is my method, and i want to apply those two values in here
Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode){
case REQUEST_LOCATION:getLocation();
break;
}
}
This is onRequestionPermission method
My rest of the code
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();
}
but seems always return back 0.0 and 0.0 ...