FusedLocationProviderClient精度在某些设备上返回1500

时间:2019-01-15 08:30:42

标签: android location fusedlocationproviderapi

我正在使用FusedLocationProviderClient来跟踪位置,并且我想通过使用getAccuracy()方法获得最佳位置。我在某些设备上检查了此方法,该方法返回1500,有时返回1700,而它必须小于几米(小于12)。为什么会发生这种情况,我该怎么办? 预先感谢。

我的代码:

mLocationCallback = new LocationCallback() {
            @Override
            public void onLocationResult(LocationResult locationResult) {
                super.onLocationResult(locationResult);
                Location location = locationResult.getLastLocation();
                float acc = location.getAccuracy();
                if (location != null && location.getAccuracy() < 10){
                    if(preLocation == null){
                        preLocation = location;
                        locationChanged(location);
                        sendLocation(location.getLatitude(), location.getLongitude());
                    }else if (preLocation.getLatitude() != location.getLatitude() && preLocation.getLongitude() != location.getLongitude()) {
                        // Logic to handle location object
                        locationChanged(location);
                        preLocation = location;
                    }
                }
            }
        };

1 个答案:

答案 0 :(得分:0)

尝试像这样将locationRequest优先级设置为高精度

LocationRequest mLocationRequest = new LocationRequest();

        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ContextCompat.checkSelfPermission(this,
                    Manifest.permission.ACCESS_FINE_LOCATION)
                    == PackageManager.PERMISSION_GRANTED) {
                //Location Permission already granted
                mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
            }
        } else {
            mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
        }
    }
 LocationCallback mLocationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(LocationResult locationResult) {
            for (Location location : locationResult.getLocations()) {
                //Do what you want with the location here
            }
        }
    };