当前,我正在使用android fusedlocation来获取经度,只要我的代码在Api级别27以下就可以正常工作,但是当Api级别在27以上时,我的代码给出了错误的位置...
private void getLocation() {
if (isContinue) {
mFusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, null);
} else {
mFusedLocationClient.getLastLocation().addOnSuccessListener(MainActivity.this, location -> {
if (location != null) {
wayLatitude = location.getLatitude();
wayLongitude = location.getLongitude();
Log.e("latlong++",String.format(Locale.US, "%s - %s", wayLatitude, wayLongitude));
checkLocationRange(wayLatitude,wayLongitude);
getCompleteAddressString(wayLatitude,wayLongitude);
// txtLocation.setText(String.format(Locale.US, "%s - %s", wayLatitude, wayLongitude));
} else {
mFusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, null);
}
});
}
}
代码以计算两个位置之间的距离
public void checkLocationRange(double endLatitude,double endLongitude)
{
Location.distanceBetween(28.6301796 ,77.3789867,endLatitude,endLongitude,dist);
float distanceInMeters = dist[0];
boolean isWithin100meter = distanceInMeters < 100;
Toast.makeText(getApplicationContext(), "Your Distance in meter="+distanceInMeters, Toast.LENGTH_LONG).show();
Log.e("isWithin100m", String.valueOf(isWithin100meter));
Log.e("distanceInMeters", String.valueOf(distanceInMeters));
if(isWithin100meter==false){
// rangestatus = false;
locationRangeDialog();
/* Toast.makeText(getApplicationContext(), "Out of range",
Toast.LENGTH_LONG).show();*/
}
else
{
rangestatus=true;
}
}