我有一个名为getLocation()的方法,借助FusedLocationClient可以找到当前设备的位置。调用此方法后,我得到了所找到位置的纬度和经度,并使用putExtra将这些值传递给另一个Activity。在getLocation()中,我有一个Toast,可以在显示的经度和纬度上打印,并且可以接收正确的位置。在活动中,我有另一个Toast,它显示了我从MainActivity发送的纬度和经度值,但结果是纬度和经度均为空。如果我返回并重新开始活动,则不会得到纬度和经度的空值。有任何想法吗?
其他代码:
case R.id.nearYou:
i = new Intent(MainActivity.this, Ricerca1.class);
if (!isGPS) {
Toast.makeText(this, "Please turn on GPS",
Toast.LENGTH_SHORT).show();
return;
}
isContinue = false;
getLocation();
latitudine = wayLatitude.toString();
longitudine = wayLongitude.toString();
i.putExtra("latPar", longitudine);
i.putExtra("lonPar", latitudine);
i.putExtra("radiusPar", "0.5");
i.putExtra("categoryPar", "Museum;Botanical_and_zoological_gardens;Churches;Cultural_centre;Cultural_sites;Historical_buildings;Library;Monument_location;Photographic_activities;Squares;Theatre;");
startActivity(i);
break;
获取:
private void getLocation() {
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},
AppConstants.LOCATION_REQUEST);
} else {
if (isContinue) {
mFusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, null);
} else {
mFusedLocationClient.getLastLocation().addOnSuccessListener(MainActivity.this, location -> {
if (location != null) {
wayLatitude = location.getLatitude();
wayLongitude = location.getLongitude();
Toast.makeText(MainActivity.this,String.format(Locale.US, "%s - %s", wayLatitude, wayLongitude), Toast.LENGTH_SHORT).show();
} else {
mFusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, null);
}
});
}
}
}
答案 0 :(得分:0)
您应该在getLocation()中的Toast消息之后启动startActivity。
为什么?
因为,getLocation等待侦听器(addOnSuccessListener)完成,因此您在完成之前开始活动。
您的代码将是:
private void getLocation() {
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},
AppConstants.LOCATION_REQUEST);
} else {
if (isContinue) {
mFusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, null);
} else {
mFusedLocationClient.getLastLocation().addOnSuccessListener(MainActivity.this, location -> {
if (location != null) {
wayLatitude = location.getLatitude();
wayLongitude = location.getLongitude();
Toast.makeText(MainActivity.this,String.format(Locale.US, "%s - %s", wayLatitude, wayLongitude), Toast.LENGTH_SHORT).show();
//Code from OTHER CODE
latitudine = wayLatitude.toString();
longitudine = wayLongitude.toString();
i.putExtra("latPar", longitudine);
i.putExtra("lonPar", latitudine);
i.putExtra("radiusPar", "0.5");
i.putExtra("categoryPar", "Museum;Botanical_and_zoological_gardens;Churches;Cultural_centre;Cultural_sites;Historical_buildings;Library;Monument_location;Photographic_activities;Squares;Theatre;");
startActivity(i);
} else {
mFusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, null);
}
});
}
}
}