Location.distanceBetween方法返回相同位置的Api级别28(pie)中的不同距离

时间:2019-03-04 07:41:12

标签: android api location fusedlocationproviderclient

当前,我正在使用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;
    }
}

0 个答案:

没有答案